繁体   English   中英

如何使用“for”循环将 plot 与 ggplot2 指向?

[英]How to plot points with ggplot2 using a « for » loop?

我开始学习 « ggplot2 » 并循环学习。 所以现在,我尝试使用“for”循环对 plot 一些点与“ggplot2”。 但是,我真的不知道该怎么做,我真的不知道这是否是个好主意。 我检查了类似的问题,我只是不明白。 也许,我需要更多的解释。

我一直在研究堆栈溢出问题,这对我帮助很大。 但是,这是我在这里的第一个问题,如果它错过了一些信息或者脚本没有正确公开,请告诉我出了什么问题,我会在下一次处理它。

这是我的 geom_point() 脚本:

    library(tidyverse)
    data("CO2")

    ggplot(data = CO2, mapping = aes(x = conc, y = uptake)) +            # I think that I must do this.

      for (i in 1:nrow(CO2)) {                                           # For each line of the dataset.
        if(CO2$Type[i] == "Quebec" & CO2$Treatment[i] == "nonchilled") { # Test these conditions.
          geom_point(mapping = aes(x = CO2$conc[i], y = CO2$uptake[i]))  # If they are true, add the point using geom_point.
        }                                                                # And eventually, I would like to add more « for » loops.
      }

我也尝试使用 annotate():

    ggplot(data = CO2, mapping = aes(x = conc, y = uptake)) +

      for (i in 1:nrow(CO2)) {
        if(CO2$Type[i] == "Quebec" & CO2$Treatment[i] == "nonchilled") {
          annotate(geom = "point", x = CO2$conc[i], y = CO2$uptake[i])
        }
      }

点只是没有出现。 我还尝试将值存储在向量中,并将它们分配给 « x » et « y » arguments。

是否有人知道如何简单地做到这一点,如果这样做很常见。 如果不是,为什么? 还有哪些选择?

谢谢你,有一个美好的一天!

我相信其他答案很好地解决了上述问题,但是如果您打算使用不同级别的类型和处理制作多个图表,您可以尝试使用facet_grid

CO2 %>%
  ggplot(mapping = aes(x = conc, y = uptake)) +
  geom_point() +
  facet_grid(. ~ Type + Treatment)

我同意 Rui Barradas 的观点,我会这样做:

CO2 %>%
  filter(Type == "Quebec" & Treatment == "nonchilled") %>% # Get nonchilled Quebec data
  ggplot(aes(x = conc, y = uptake)) +                      # Plot concentration and uptake
    geom_point()                                           # Plot as points

我认为您不希望在 ggplot function 中有 for 循环。 我建议将您的 dataframe 过滤到您之前想要的条件,然后绘制所有这些点。

您必须列出您想要的所有条件并过滤掉,以使 dataframe 仅包含您想要的观察结果 plot。 见下文:

CO2_quebec <- CO2 %>% 
  filter(Type=="Quebec") %>% #Filters out Type only in 'Quebec' 
  filter(Treatment == "nonchilled") #Filters out Treatment only for 'nonchilled' 

ggplot(data = CO2_quebec, mapping = aes(x = conc, y = uptake)) +  
  geom_point() #Note, your geom_point takes your x and y based on your ggplot line above unless otherwise specified 

这是 ggplot 的一个非常常见的用法。

以下是您可能正在寻找的内容:

gg<- ggplot(data = CO2[CO2$Type=="Quebec" & CO2$Treatment=="nonchilled",], mapping = aes(x = conc, y = uptake))  + 
  geom_point()
gg

首先,您应该在执行 plot 之前过滤您的数据。 它会让你的生活更轻松(就像我一样)。 然后,ggplot 是一个聪明的 package:你不需要精确到 plot 的每个点。 如果你什么都不告诉他,它会明白你想要 plot 一切(这就是为什么过滤之前有用)。

此外,您可能会欣赏以下内容:

gg<- ggplot(data = CO2[CO2$Treatment=="nonchilled",], mapping = aes(x = conc, y = uptake, group=Type,color=Type))  + 
  geom_point()
gg

你把事情复杂化了。 尝试filter

  library(tidyverse)
  data("CO2")

  CO2 %>% filter(Type == 'Quebec' & Treatment == 'nonchilled') %>% 
  ggplot(aes(x = conc, y = uptake)) + 
    geom_point()  

暂无
暂无

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

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