Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

How to plot factored variable in R

$
0
0

I have my dataframe like this:

ind.heights <- structure(list(names = structure(c(3L, 4L, 5L, 1L, 2L), .Label = c("bagmati", 
"bardiya", "Jumla", "kalikot", "ramechap"), class = "factor"), 
    heights = c(5.6, 4.5, 6.1, 5.5, 5.1), placesSD = c(2, 2, 
    2, 2, 2), lower_boundary = c(3.84695491884684, 2.74695491884684, 
    4.34695491884684, 3.74695491884684, 3.34695491884684), upper_boundary = c(7.35304508115316, 
    6.25304508115316, 7.85304508115316, 7.25304508115316, 6.85304508115316
    )), row.names = c(NA, -5L), class = "data.frame")

I tried to plot scatter plot for this data as below:

plot(
  # x= names, y = heights,
  heights ~ names,
  data = ind.heights,
  col = "blue",
  pch = 19,
  main = "Heights",
  ylab = "Heights",
  ylim = c(
    min(ind.heights$lower_boundary),
    max(ind.heights$upper_boundary)
  )
)

The problem here is that my variable is a factor and won't plot properly, but if I change the variable with numerical values and run the same code, I get the desired plot.

ind.heights$names <- 1:5
plot(
      # x= names, y = heights,
      heights ~ names,
      data = ind.heights,
      col = "blue",
      pch = 19,
      main = "Heights",
      ylab = "Heights",
      ylim = c(
        min(ind.heights$lower_boundary),
        max(ind.heights$upper_boundary)
      )
    )

How do I plot this with the factor with original names?


Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>