Reputation: 283
I want to plot a histogram and label each bin by the average of some other variable.
library(dplyr)
data(mtcars)
meanwt=mtcars %>% group_by(carb) %>%
dplyr::summarize(meanwt = mean(wt)) %>% pull(meanwt)
g=
ggplot(data=mtcars, aes(x=carb, y=..count..)) +
geom_histogram(alpha=0.3, position="identity", lwd=0.2,binwidth=1)+
theme_bw()+
theme(panel.border = element_rect(colour = "black", fill=NA, size=0.7))
It works well when I plot the bin count
##plot the count of bin
g+stat_count(aes(y=..count..,label=..count..),geom="text",vjust=-1)
However, If I want to label mean of some other variable then it doesnt work.
#plot mean of some other variable
g+stat_summary(aes(x=carb,y=wt),xfun.y = "mean", colour = "Black", size = 2,
geom = "text",position=position_dodge(width=0.9), vjust=-0.25,label = meanwt)
Can any one help me with this?
Upvotes: 1
Views: 7278
Reputation: 5716
I am not sure why Aleksandr's suggestion did not work. As an alternative, you could use this:
dat <- mtcars %>% select(carb, wt) %>%
group_by(carb) %>% mutate(mean_wt = mean(wt), carb_count = n())
g + geom_text(data=dat, aes(carb, carb_count+0.5, label=mean_wt), color="black", check_overlap = TRUE)
Based on Tung's suggestion!
Upvotes: 2
Reputation: 283
I feel like answering my own question by combining help from @Aleksandr and @Prradep. I am not sure if this is super easy to read but for a newbie like me would help.
##reading the library and data
library(dplyr)
data(mtcars)
##aggregating the data (for some strange reason I have to use dplyr:: everytime I use a function within piping)
dat <- mtcars %>% dplyr::select(carb, wt) %>%
dplyr::group_by(carb) %>% dplyr::mutate(mean_wt = mean(wt), carb_count = n())
##plotting the data
ggplot(data=mtcars, aes(x=carb, y=..count..)) +
geom_histogram(alpha=0.3, position="identity", lwd=0.2,binwidth=1)+
theme_bw()+
theme(panel.border = element_rect(colour = "black", fill=NA, size=0.7))+
geom_text(data=aggregate(mean_wt~carb+carb_count,dat,mean), aes(carb, carb_count+0.5, label=round(mean_wt,1)), color="black")
Upvotes: 0
Reputation: 1914
First of all, it is good to know what you are dealing with and what you want to achieve. In this case you want to find how many cars have 1, 2, ... , 8 cylinders. So all you need to do is to group by cyl and find how many cars drops in that category:
mm <- mtcars %>%
group_by(carb) %>%
summarise(n = length(wt))
Next you may want to use simple bar chart to plot cyl against number of occurrences (n):
ggplot(data=mm, aes(x=carb, y=n)) +
geom_bar(stat="identity", width=0.5, position=position_dodge(), fill = "steelblue") +
geom_text(aes(label=n), vjust=1.5, color="white",
position = position_dodge(0.9), size=4)+
scale_x_continuous(breaks = seq(min(mm$carb), max(mm$carb), 1)) +
scale_y_continuous(breaks = seq(min(mm$n), max(mm$n), 1)) +
theme_minimal()
Upvotes: 1