小编out*_*een的帖子

ggplot 中每个面都有不同的垂直线

我想在 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)
Run Code Online (Sandbox Code Playgroud)

但是当使用小平面时,会显示 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) …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

4
推荐指数
1
解决办法
1815
查看次数

标签 统计

ggplot2 ×1

r ×1