我在R中有一个数据框。第一列是日期。其余列是每个类别的数据。
library(tidyverse)
mydf <- tribble(~Date, ~View1, ~View2, ~View3, ~View4, ~View5, ~View6,
'2010-05-17', 13, 10, 13, 10, 13, 10,
'2010-05-18', 11, 11, 13, 10, 13, 10,
'2010-05-19', 4, 12, 13, 10, 13, 10,
'2010-05-20', 2, 10, 13, 10, 13, 10,
'2010-05-21', 23, 16, 13, 10, 13, 10,
'2010-05-22', 26, 15, 13, 10, 13, 10
)
如何绘制两条线的时间图?每列的每一行。即View1的一行,View2的一行,View3的一行,依此类推。 x轴是日期。 ggplot中有功能可以轻松实现吗?
我搜索了其他帖子,在下面看到了一个解决方案,但这在情节上没有任何帮助。
mydf %>%
gather(key,value, View1, View2, View3, View4, View5, View6 ) %>%
ggplot(aes(x=Date, y=value, colour=key))
我还尝试了以下命令。
test_data_long1 <- melt(mydf, id="Date")
ggplot(data=test_data_long1,
aes(x=date, y=value, colour=variable)) +
geom_line()
这给我一个错误。
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Error: All columns in a tibble must be 1d or 2d objects:
* Column `x` is function
答案 0 :(得分:0)
几种方法...
mydf %>%
mutate(Date = as.Date(Date)) %>%
ggplot(aes(x = Date)) +
geom_line(aes(y = View1), colour = 'red') +
geom_line(aes(y = View2), colour = 'blue')
mydf %>%
mutate(Date = as.Date(Date)) %>%
gather(-Date, key = 'View', value = 'value' ) %>%
ggplot(aes(x=Date, y=value, colour=View)) +
geom_line()