简体   繁体   English

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

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

I started to learn « ggplot2 » and loops since some times.我开始学习 « ggplot2 » 并循环学习。 So now, I try to plot some points with « ggplot2 » using a « for » loop.所以现在,我尝试使用“for”循环对 plot 一些点与“ggplot2”。 But, I don't really know how to do it, and I don't really know if it's a good idea.但是,我真的不知道该怎么做,我真的不知道这是否是个好主意。 I checked over the similar questions and I just don't understand.我检查了类似的问题,我只是不明白。 Maybe, I need more explications.也许,我需要更多的解释。

I've been surfing on stack overflow for a while and it helped me a lot.我一直在研究堆栈溢出问题,这对我帮助很大。 However, it's my first question here and if it miss some informations or if the script is not correctly exposed, just tell me what's wrong and I'll take care it for the next time.但是,这是我在这里的第一个问题,如果它错过了一些信息或者脚本没有正确公开,请告诉我出了什么问题,我会在下一次处理它。

Here's my script with geom_point():这是我的 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.
      }

And I also try with annotate():我也尝试使用 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])
        }
      }

The points just don't appear.点只是没有出现。 I also try to stock the values in vectors and assigned them to the « x » et « y » arguments.我还尝试将值存储在向量中,并将它们分配给 « x » et « y » arguments。

Is someone knows how to do that simply, and if it's common to do that.是否有人知道如何简单地做到这一点,如果这样做很常见。 If it's not, why?如果不是,为什么? And what are the alternatives?还有哪些选择?

Thank you and have a good day!谢谢你,有一个美好的一天!

I believe the other answers solve the stated question well, but if you plan to make multiple graphs using the different levels of Type & Treatment, you might try using facet_grid :我相信其他答案很好地解决了上述问题,但是如果您打算使用不同级别的类型和处理制作多个图表,您可以尝试使用facet_grid

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

I agree with Rui Barradas, I would do something like this:我同意 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

I don't think you'll want to have the for loop within the ggplot function.我认为您不希望在 ggplot function 中有 for 循环。 I would recommend filtering your dataframe to the conditions you want before, and then plotting all those points.我建议将您的 dataframe 过滤到您之前想要的条件,然后绘制所有这些点。

You'll have to come up with a list of all the conditions you want and filter out to make a dataframe of only those observations you want to plot.您必须列出您想要的所有条件并过滤掉,以使 dataframe 仅包含您想要的观察结果 plot。 See below:见下文:

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 

It is a very common usage of ggplot.这是 ggplot 的一个非常常见的用法。

Here is what you are probably looking for:以下是您可能正在寻找的内容:

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

First of all, you should filter your data before doing the plot.首先,您应该在执行 plot 之前过滤您的数据。 It will make your life easier (as I did).它会让你的生活更轻松(就像我一样)。 Then, ggplot is a clever package: you don't need to precise every point you want to plot.然后,ggplot 是一个聪明的 package:你不需要精确到 plot 的每个点。 If you don't tell him anything, it will understand that you want to plot everything (that's why it is useful to filter before).如果你什么都不告诉他,它会明白你想要 plot 一切(这就是为什么过滤之前有用)。

Moreover, here is something you will probably appreciate:此外,您可能会欣赏以下内容:

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

You're over complicating this.你把事情复杂化了。 Try filter :尝试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