我想在ggplot图形的每个方面显示一个垂直条,其中包含该方面数据的平均值.我可以针对某个方面这样做:
# For Petal Length Only :
Mean <- colMeans(iris["Petal.Length"])
iris %>% pivot_longer(cols = !Species,names_to = "Characteristic",values_to = "Value") %>%
filter(Characteristic=="Petal.Length") %>%
ggplot(aes(x=Value,fill=Species))+geom_density()+facet_wrap(~Characteristic)+
geom_vline(xintercept = Mean)
如果每个面显示4条线,则每个面显示4条线,而不是垂直面:
# For all characteristics :
Mean <- colMeans(iris[-5])
iris %>% pivot_longer(cols = !Species,names_to = "Characteristic",values_to = "Value") %>%
ggplot(aes(x=Value,fill=Species))+geom_density()+facet_wrap(~Characteristic)+geom_vline(xintercept = Mean)
# Display vertical lines for all means where I would like only one vertical bar
# by group displayed (one for petal length, one for sepal width, etc)
# for example for Petal Length, only the vertical line with intercept colMeans(iris["Petal.Length"]) should be displayed.