ggplot 双线图和 3 列

ggplot double linechart and 3 columns

我通过 Rfacebook 包提取了一些关于 Facebook 页面的数据。我想要得到的是两个并排的折线图,就像附加的图像(每个用户名的折线图)一样,我可以像附加的图像一样同时显示喜欢、评论和分享。

FromUser  time      shares  comments    likes
User 1    17-01-18  67      5           100
User 2    16-01-18  46      13          65
User 1    16-01-18  32      25          32
User 1    15-01-18  45      36          45
User 2    14-01-18  64      52          58

我尝试了以下代码,但我没有用。有什么建议吗?

line_chart <- ggplot(dataframe, aes(x = time)) + 
  geom_line(aes(y = shares), colour="blue") + 
  geom_line(aes(y = comments), colour = "grey") +
  geom_line(aes(y = likes), colour = "grey") +  
  ylab(label="Number of interactions") + 
  xlab("Days")

通过 tidyr:

稍微重新排列您的数据,可能更容易创建当前图
library(tidyr)
#use gather to make a metric column
dataframe.2 <- dataframe %>% gather(Metric, Value, -time, -FromUser)

#convert date for easy ploting
dataframe.2$time <- as.Date(dataframe.2$time , format = "%d-%m-%y")

每个用户、时间和指标给出一条记录:

FromUser       time   Metric Value
User 1 2018-01-17   shares    67
User 2 2018-01-16   shares    46
User 1 2018-01-16   shares    32
User 1 2018-01-15   shares    45
User 2 2018-01-14   shares    64
User 1 2018-01-17 comments     5
User 2 2018-01-16 comments    13
User 1 2018-01-16 comments    25
User 1 2018-01-15 comments    36
User 2 2018-01-14 comments    52
User 1 2018-01-17    likes   100
User 2 2018-01-16    likes    65
User 1 2018-01-16    likes    32
User 1 2018-01-15    likes    45
User 2 2018-01-14    likes    58

现在输入 ggplot:

ggplot(data = dataframe.2, aes(x = time, y = Value)) + 
  geom_line(aes(color = Metric, group = Metric)) +facet_wrap(~FromUser) + 
  xlab("Date") + ylab("Number of interactions")

最终剧情: