简体   繁体   English

在刻面上添加不同的垂直线 ggplot2

[英]Adding different vertical lines on facets ggplot2

I am quite a newbie to RStudio and I am having problems adding different vertical lines on each of my two facets using facet_wrap我是 RStudio 的新手,我在使用 facet_wrap 在我的两个方面添加不同的垂直线时遇到问题

Here is what I thought:这是我的想法:

library(ggplot2)

g <- ggplot(dat, aes(x=index, na.rm= TRUE))

d <- g+ geom_density() + facet_wrap(~gender)

vline.data <- data.frame(z = c(2.36,2.48),gender = c("Female","Male")) 

d1 <- d + geom_vline(aes(xintercept = z),vline.data)

But it adds the same two lines to each facet - what would you reckon is the problem?但它在每个方面都添加了相同的两行 - 你认为问题是什么? I have thought of somehow splitting the facets into two separate data frames, but I have no idea how to go on about it.我曾想过以某种方式将方面分成两个单独的数据帧,但我不知道如何 go 关于它。

PS The x-axis (the index) goes from 1 to 4. PS x 轴(索引)从 1 变为 4。

Thank you in advance.先感谢您。

        index  <-  c(NA, NA, 4, 4, 4, NA, NA, NA, NA, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
        3, NA, 3, NA, NA, 3, 4, 4, 4, 4, 4, 3, 4, 3, 4, 3, NA, 4, 2, 
        4, 4, 2, 2, NA, 2, 3, 3, 2, 2, NA, NA, 2)

       gender <-  c("Female", "Female", "Male", "Male", "Male", "Female", "Female", 
        "Male", "Female", "Male", "Female", "Male", "Female", "Male", 
        "Male", "Female", "Female", "Female", "Male", "Female", "Female", 
        "Male", "Male", "Female", "Female", "Female", "Male", "Male", 
        "Female", "Female", "Male", "Female", "Female", "Female", "Male", 
        "Male", "Female", "Male", "Male", "Female", "Female", "Male", 
        "Male", "Female", "Female", "Male", "Male", "Male", "Male", "Male")

Using the code and data above I get this plot使用上面的代码和数据,我得到了这个 plot

This was asked a long time ago, but here's the answer:很久以前有人问过这个问题,但这是答案:

You need to add geom_vline to your ggplot.您需要将geom_vline添加到您的 ggplot 中。 geom_vline takes a single value, not a vector, and it doesn't iterate, so you can't have multiple values in a separate data object from the rest of your data. geom_vline采用单个值,而不是向量,并且它不会迭代,因此您不能在来自数据的 rest 的单独数据 object 中有多个值。 Merge vline.data as a new column with the rest of dat as is appropriate for your data set. vline.data作为新列与dat的 rest 合并为适合您的数据集的列。 This question doesn't have reproducible data, but put index , gender and however you calculate vline.data into a single dat dataframe with each of those pieces as column names.这个问题没有可重复的数据,但是将indexgender以及您计算的vline.data放入单个dat dataframe 中,其中每一个都作为列名。 Then you can summarise the vline.data .然后你可以summarise vline.data The example below assumes the values within vline.data are the mean values for the entire index column.下面的示例假定vline.data中的值是整个index列的平均值。

p <- ggplot(dat, aes(x=index, na.rm= TRUE)) +
  geom_density() + 
  facet_wrap(. ~ gender) +
  geom_vline(data = . %>% group_by(gender) %>% summarise(vl=mean(index)), 
             aes(xintercept=vl))

How to Add Lines With A Facet R 如何使用 Facet R 添加行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM