如何自定义添加垂直线到ggplot方面功能? [英] How to custom add vertical lines to ggplot facet function?

查看:47
本文介绍了如何自定义添加垂直线到ggplot方面功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中每个物种都以某种密度(数字)和另一物种的类型(数字)混合.我想在ggplot中的每个facet_grid面板中添加两种垂直线:(a)固定密度/类型的固定线.例如1000/1 = 1000、1000/6 = 166.7、10000/1 = 10000、10000/6 = 1666.7

(b)直方图上叠加的每种处理的自举平均值和置信区间.我尝试使用geom_vline添加平均值,但这似乎不正确.看起来所有手段都是一样的

  set.seed(111)计数<-rbinom(500,100,0.1)物种<-rep(c("A","B"),时间= 250)密度< -rep(c("1000","10000","1000","10000"),时间= 125)类型<-rep(c("1","1","6","6"),时间= 125)df<-data.frame(种类,密度,类型,数量)#我太天真了,但是我无法满足所有要求.加.ggplot(df,aes(x =计数,颜色=种类,填充=种类))+geom_histogram(position ="identity",alpha = 0.5)+theme_bw()+ ylab(频率")+facet_grid(种类〜类型+密度)+主题(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+主题(legend.position ="none")+主题(aspect.ratio = 1.75/1)+geom_vline(aes(xintercept = mean(count)),color ="blue",linetype ="dashed",size = 1) 

解决方案

我不确定为什么这是分面的默认行为,但我认为解决方法是将汇总数据传递给 geom_vline .一种方法如下(在最后一学期更改您的代码)...

  ggplot(df,aes(x =计数,颜色=种类,填充=种类))+geom_histogram(position ="identity",alpha = 0.5)+theme_bw()+ ylab(频率")+facet_grid(种类〜类型+密度)+主题(legend.position ="none")+主题(aspect.ratio = 1.75/1)+geom_vline(data = function(x)x%>%group_by(种类,类型,密度)%>%summarise(平均计数=平均值(计数)),aes(xintercept = meancount),颜色=蓝色",线型=虚线",尺寸= 1) 

(我已经恢复了网格线,以便您可以看到网格线确实有一点移动!)

I have a dataset where each species was mixed with a certain density (numeric) and type (numeric) of another species. I want to add two types of vertical lines to each of my facet_grid panels in ggplot: (a) A fixed line which dives the density/ type. e.g. 1000/1 = 1000, 1000/6 = 166.7, 10000/1 = 10000, 10000/6 = 1666.7

(b) The bootstrapped mean AND confidence interval for each treatment overlayed on the histogram. I tried adding the mean using geom_vline but it doesn't seem right. It looks like all the means are identical

set.seed(111)
count <- rbinom(500,100,0.1) 
species <- rep(c("A","B"),time = 250)
density <- rep(c("1000","10000","1000","10000"),time = 125)
type <- rep(c("1","1","6","6"),time = 125)
df <- data.frame(species, density, type, count) # I feel too naiive, but I'm not able to get all the treatments filled. Gah.

ggplot(df, aes(x= count, colour = species, fill = species)) + 
   geom_histogram(position="identity", alpha=0.5) + 
   theme_bw() + ylab("Frequency") + 
   facet_grid(species ~ type + density) + 
   theme(panel.grid.major = element_blank(),
         panel.grid.minor = element_blank()) +
   theme(legend.position = "none") + theme(aspect.ratio = 1.75/1) + 
   geom_vline(aes(xintercept=mean(count)),color="blue", linetype="dashed", size=1)

解决方案

I'm not sure why this is the default behaviour on faceting, but I think the way round it is to pass summarised data to geom_vline. One way is as follows (changes to your code in the final term)...

ggplot(df, aes(x= count, colour = species, fill = species)) + 
  geom_histogram(position="identity", alpha=0.5) + 
  theme_bw() + ylab("Frequency") + 
  facet_grid(species ~ type + density) + 
  theme(legend.position = "none") + theme(aspect.ratio = 1.75/1) + 
  geom_vline(data = function(x) x %>% 
               group_by(species, type, density) %>% 
               summarise(meancount = mean(count)),
             aes(xintercept=meancount),color="blue", linetype="dashed", size=1)

(I've reinstated the grid lines so that you can see that the line does move a little bit!)

这篇关于如何自定义添加垂直线到ggplot方面功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆