2d图为空(第2个线和第三个变量的线颜色)

时间:2018-12-17 11:20:04

标签: r ggplot2

我想要一个情节,其中相同关键字的所有行都相互连接,并且时间戳在X轴上,而计数在Y轴上。我希望每个关键字的行和用关键字标记的行都有不同的颜色。

感谢@ Roman,我设法购买了以下产品:

#load from csv. Fields are keyword,count,timestamp. Example line: "blablabla","123","2018/08/09"
csvdata <- read.csv("c:/mydataset.csv", header=TRUE, sep=",")

# merge fields into a common dataframe
xy <- data.frame(time, word = as.factor(csvdata$keyword), count = rpois(length(time), lambda = 5))

library(ggplot2)

# draw the chart
ggplot(xy, aes(x = time, y = count, color = word)) +
  theme_bw() +
  scale_color_brewer(palette = "Set1") +  # choose appropriate palette
  geom_line()

它确实创建了画布和正确的图例。但是,未绘制点/线。我在做什么错了?

CSV的输出:(因匿名而撤消)

   > dput(csvdata)
structure(list(keyword = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 7L, 2L, 7L, 
2L, 7L, 2L, 7L, 2L, 7L, 2L, 2L, 7L, 2L, 7L, 10L, 10L, 10L, 10L, 
10L, 6L, 8L, 9L, 10L, 6L, 8L, 9L, 6L, 8L, 9L, 6L, 8L, 9L, 3L, 
6L, 3L, 3L, 3L, 3L), .Label = c("word word durch moreWords", 
"word word mit moreWords", "word word via otherWord", 
"word word moreWords", "word word otherWord", 
"word word otherWord", "word word von moreWords", 
"otherWord moreWords", "otherWord otherWord", 
"otherWord otherWord "), class = "factor"), count = c(3L, 
2L, 3L, 1L, 6L, 6L, 2L, 3L, 2L, 1L, 2L, 6L, 3L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 2L, 6L, 1L, 4L, 5L, 5L, 7L, 3L, 5L, 4L, 4L, 3L, 3L, 
7L, 5L, 7L, 4L, 5L, 2L, 3L, 2L, 3L, 6L, 8L, 11L, 9L, 10L, 1L, 
4L, 1L, 3L, 2L, 5L, 2L, 2L, 2L, 2L, 2L, 3L, 8L, 3L, 2L, 3L, 3L, 
2L, 3L, 3L, 6L, 5L, 2L, 13L, 3L, 6L, 2L, 9L, 5L, 14L, 8L, 6L, 
4L, 1L, 7L, 4L, 1L, 7L, 9L, 2L, 4L, 2L, 2L, 3L, 2L, 2L, 7L, 4L, 
6L, 3L, 1L, 1L, 3L, 2L, 3L, 1L, 1L, 1L, 6L, 2L, 1L, 4L, 6L, 5L, 
6L, 3L, 3L, 6L, 1L, 5L, 2L, 1L, 1L, 2L), timestamp = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 
81L, 82L, 83L, 83L, 84L, 84L, 85L, 85L, 86L, 86L, 87L, 87L, 88L, 
89L, 89L, 90L, 90L, 90L, 91L, 92L, 93L, 94L, 95L, 95L, 95L, 95L, 
96L, 96L, 96L, 97L, 97L, 97L, 98L, 98L, 98L, 99L, 99L, 100L, 
101L, 102L, 103L), .Label = c("2018/08/09", "2018/08/10", "2018/08/11", 
"2018/08/12", "2018/08/13", "2018/08/14", "2018/08/15", "2018/08/16", 
"2018/08/17", "2018/08/18", "2018/08/19", "2018/08/22", "2018/08/23", 
"2018/08/24", "2018/08/25", "2018/08/26", "2018/08/29", "2018/08/30", 
"2018/08/31", "2018/09/01", "2018/09/03", "2018/09/06", "2018/09/13", 
"2018/09/14", "2018/09/15", "2018/09/16", "2018/09/17", "2018/09/18", 
"2018/09/19", "2018/09/20", "2018/09/21", "2018/09/22", "2018/09/23", 
"2018/09/24", "2018/09/25", "2018/09/26", "2018/09/27", "2018/09/28", 
"2018/09/29", "2018/09/30", "2018/10/01", "2018/10/02", "2018/10/03", 
"2018/10/04", "2018/10/05", "2018/10/06", "2018/10/07", "2018/10/09", 
"2018/10/10", "2018/10/11", "2018/10/12", "2018/10/13", "2018/10/14", 
"2018/10/15", "2018/10/16", "2018/10/17", "2018/10/18", "2018/10/19", 
"2018/10/25", "2018/10/28", "2018/10/29", "2018/10/30", "2018/10/31", 
"2018/11/01", "2018/11/02", "2018/11/03", "2018/11/04", "2018/11/05", 
"2018/11/06", "2018/11/07", "2018/11/09", "2018/11/10", "2018/11/11", 
"2018/11/12", "2018/11/13", "2018/11/14", "2018/11/15", "2018/11/16", 
"2018/11/17", "2018/11/18", "2018/11/19", "2018/11/20", "2018/11/21", 
"2018/11/22", "2018/11/23", "2018/11/24", "2018/11/25", "2018/11/26", 
"2018/11/27", "2018/11/28", "2018/11/29", "2018/12/02", "2018/12/04", 
"2018/12/05", "2018/12/06", "2018/12/07", "2018/12/08", "2018/12/09", 
"2018/12/12", "2018/12/13", "2018/12/14", "2018/12/15", "2018/12/16"
), class = "factor")), .Names = c("keyword", "count", "timestamp"
), class = "data.frame", row.names = c(NA, -121L))

1 个答案:

答案 0 :(得分:0)

我无法使用您提供的数据进行复制。它似乎格式错误。 通过一些虚拟数据,我可以生成您描述的图

library(ggplot2)

# create dummy data
xy <- data.frame(word=as.factor(c('a','a','b','b','c','c')),time=as.Date(c('2018/08/09','2018/08/06','2018/08/07','2018/08/08','2018/08/05','2018/08/03')), count = c(123,241,191,301,199,221))

# draw the chart
ggplot(xy, aes(x = time, y = count, color = word)) +
theme_bw() +
scale_color_brewer(palette = "Set1") +  # choose appropriate palette
geom_line()

也许您可以看看您的数据框xy是否看起来像您期望的那样,以及列名是否与您在ggplot()命令中指定的列名相同