facet_wrap获取列数

问题描述 投票:0回答:1

有一点我无法解决的问题。在我的数据集中,我有三列(pluginUserID,类型,时间戳),我想为每个pluginUserID创建一个带facet wrap的ggplot。我的数据集看起来像这样,只有更多的用户。

pluginUserID  type     timestamp
3             follow   2015-03-23
3             follow   2015-03-27
43            follow   2015-04-28

所以在下一步我想创建一个带有facet包的ggplot,所以我的代码看起来像这样。

timeline.plot <- ggplot(
    timeline.follow.data,
    aes(x=timeline.follow.data$timestamp, y=timeline.follow.data$type)
) + geom_bar(stat = "identity") + 
facet_wrap(~timeline.follow.data$pluginUserID) + 
theme(axis.title.x=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank()
)

如果我要查看我的情节,它看起来像这样。

如你所见,在y轴上没有可读的单位,这就是我想要做的。我想要想象每天和每个pluginUser的跟随数量。并且在y轴上应该是一个单位。

r ggplot2 facet facet-wrap
1个回答
1
投票

当我看到你的数据集时,我会在想象它之前做一件事。

timeline.follow.data<- timeline.follow.data %>%
count(pluginUserID, type, timestamp)

如果您的数据如下所示:

pluginUserID  type     timestamp
3             follow   2015-03-23
3             follow   2015-03-27
3             follow   2015-03-27
43            follow   2015-04-28
43            follow   2015-04-28

计数功能后:

pluginUserID  type     timestamp    n
3             follow   2015-03-23   1
3             follow   2015-03-27   2
43            follow   2015-04-28   2  

等等。

然后使用ggplot函数:

timeline.plot <- ggplot(
    timeline.follow.data,
    aes(x=timeline.follow.data$timestamp, y=timeline.follow.data$n)
) + geom_bar(stat = "identity") + 
facet_wrap(~timeline.follow.data$pluginUserID) + 
theme(axis.title.x=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank()
)

n意味着你想要的,对于选定的用户和一天有多少关注。希望它有帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.