Home > OS >  Adding legend and changing scale on figure
Adding legend and changing scale on figure

Time:10-09

I am trying to add a legend for the mean and median to my histogram. I am also trying to change the scale on the y-axis that is labeled count. It is currently showing the density scale. I want the density plot but the count scale. Alternatively, I would be fine with a second scale or the counts at the end of the histogram. I am just not sure how to go about it. Below is some data and the current code. Thank you in advance.

studyData=data.frame(X=rchisq(1:100000, df=3))
colnames(studyData) <- "hoursstudying"

mu <- data.frame(mean(studyData$hoursstudying))
colnames(mu) <- "Mean"
med <- data.frame(median(studyData$hoursstudying))
colnames(med) <- "Median"

p <- ggplot(studyData, aes(x = hoursstudying))  
  geom_histogram(aes(y=(..density..)), binwidth = 1, colour = "black", fill = "lightblue")  
  geom_density(alpha=.2, fill="#FF6666")  
  geom_vline(data = mu, aes(xintercept = Mean),
             color = "red", linetype = "dashed", size = 1)  
  geom_vline(data = med, aes(xintercept = median(Median)),
             color = "purple", size = 1)   
  labs(title = "Hours Spent Completing Course Work")  
  ylab("Count")  
  xlab("Hours Studying")
  theme(plot.title = element_text(hjust = 0.5))
p


enter image description here

CodePudding user response:

You can access the count instead of density on the y axis much in the same way you reference the internal calculation of density using the "..XXXX.." notation. In this case, use ..count...

You will need to change both y aesthetics for geom_histogram() and geom_density():

ggplot(studyData, aes(x = hoursstudying))  
  geom_histogram(aes(y=(..count..)), binwidth = 1, colour = "black", fill = "lightblue")  
  geom_density(aes(y=..count..), alpha=.2, fill="#FF6666")  
  # ... everything else is the same

enter image description here

Note: also, I echo the comment from u/Limey. The median and mean values in your original plot shared are clearly wrong... yet when I run the code I am getting the values looking correct. Not sure what that's about, OP, but perhaps that's a different question.

CodePudding user response:

Since @chemdork123 answered the question about the y-axis scale I won't say anything about it. To add the median/mean values to the legend you need to add them as aesthetics.

p <- ggplot(studyData, aes(x = hoursstudying))  
  geom_histogram(aes(y=(..density..)), binwidth = 1, colour = "black", fill = "lightblue")  
  geom_density(alpha=.2, fill="#FF6666")  
  geom_vline(data = mu, aes(xintercept = Mean,
                            color = "red"),
             linetype = "dashed", size = 1)  
  geom_vline(data = med, aes(xintercept = Median,
                             color = "purple"),
             size = 1)   
  scale_color_manual(values = c("purple", "red"),
                     labels = c("Median", "Mean"))  
  labs(title = "Hours Spent Completing Course Work")  
  ylab("Count")  
  xlab("Hours Studying")  
theme(plot.title = element_text(hjust = 0.5))

enter image description here

  • Related