简体   繁体   中英

facet_wrap get count of column

There's a little problem that I'm not able to solve. In my dataset I have three columns (pluginUserID, type, timestamp) and I want to create a ggplot with facet wrap for every pluginUserID. My dataset looks like this, just with more users.

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

So in the next step I wanted to create a ggplot with a facet wrap, so my code looks like this.

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()
)

If I'm going to view my plot, it looks like this.

As you can see, on the y axis there's no unit to read and that's what I want to do. I want to visualise the number of follows per day and per pluginUser. And on the y axis should be a unit.

as I see your dataset I would do one thing before visualize it- count.

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

if your the data looks like this:

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

after count function:

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

and so on.

Then use ggplot function:

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 would mean as you wanted, how many follows was for selected user and day. Hope it helped :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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