简体   繁体   English

在for循环中以每次迭代的不同列打印ggplot

[英]Print ggplot in for loop with different columns per iteration

I am trying to use ggplot2 to create multiple graphs for my data using a for loop. 我正在尝试使用ggplot2使用for循环为我的数据创建多个图形。 For this I am trying to print the ggplot object which returns an error 为此,我试图打印ggplot对象,该对象返回错误

Error in [.data.frame (df, , (i)) : undefined columns selected . [.data.frame (df,,(i))中的错误:选择了未定义的列。

Also I am unable to get a legend to be printed on the graph. 另外,我无法在图上打印图例。 If I don't print it shows a single chart. 如果我不打印,它会显示一个图表。

for(i in seq(from=2, to=ncol(pvp), by=4)){
  sq= as.numeric(c(1,(i):(i+3)))

  print(sq)
  df=pvp[,sq]
  print(head(df))
  colordf=colnames(df)[((i):(i+3))]


  p=ggplot(df,aes(x=df$tot_urls_read)) + 
    geom_line(aes(y=df[,(i)]),color="red")
  p=p+ geom_line(aes(y=df[,(i+1)]),color="green")
  p=p+ geom_line(aes(y=df[,(i+2)]),color="blue")
  p=p+ geom_line(aes(y=df[,(i+3)]),color="black") + 
    xlab("Percentage") + 
    ylab("Pvs") + 
    scale_fill_identity(name = '', guide = 'legend',labels = colordf) +
    scale_colour_manual(name = 'Topic lines', values =c('red'='red','green'='green','blue'='blue','black'='black'), 
                        labels = colordf)
  print(p)
}

This is a part of my data . 这是我数据的一部分。 There are more columns in it 其中有更多列

  tot_urls_read Andhra       Goa Maharashtra Karnataka        UP        MP      West    Bengal Assam
1             1      1 100.00000   100.00000 100.00000 100.00000 100.00000 100.00000 100.00000   100
2             2      2  51.28552    50.25325  50.00000  50.00000  50.00000  51.95487  50.70178    50
3             3      3  34.70799    33.67254  33.33333  33.33333  33.33333  35.23031  33.90084    33
4             4      4  26.28165    25.26571  25.00000  25.00000  25.00000  26.73067  25.36423    25
5             5      5  21.18226    20.31540  20.00000  20.00000  20.00000  21.62096  20.48651    22
6             6      6  17.83460    16.92501  16.66667  16.66667  16.76647  18.20869  16.99758    16

在此处输入图片说明

How can I use a for loop to create multiple charts and also give a header? 如何使用for循环创建多个图表并提供标题? I don't have a hang of this and seem to be all over the place. 我对此一无所知,似乎无处不在。

Any Help is greatly appreciated. 任何帮助是极大的赞赏。

To add a legend, you need to properly map the corresponding aesthetics. 要添加图例,您需要正确映射相应的美学。 Instead of plotting four different geom_lines, reshape your data to long form and plot all lines within the same call: 无需绘制四个不同的geom_lines,而是将数据重塑为长格式并在同一调用中绘制所有线条:

library(ggplot2)

for(i in seq(from=2, to=ncol(pvp), by=4)){
  sq= as.numeric(c(1,(i):(i+3)))

  df=pvp[,sq]

  library(tidyr)
  dft <- gather(df,key, value,-tot_urls_read)

  p=ggplot(dft,aes(x=tot_urls_read)) + 
    geom_line(aes(y=value,color=key)) +
    xlab("Percentage") + 
    ylab("Pvs") +
    scale_color_manual(values = c("red","green","blue","black"))
  print(p)
}

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

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