奇怪的Y轴躲闪条形图

时间:2022-10-19 14:06:18

I want to compare the pageviews and sessions in each months.

我想比较每个月的综合浏览量和会话数。

So, I got a data frame as below

所以,我有一个数据框如下

month time_interval sessions pageviews
1   < 10s   564622  577686
1   11 ~ 30 36575   84314
1   31~60   46547   127134
1   61~180  106056  408649
1   181~600 125891  839148
1   601~1800    99293   1143019
1   >1801   38534   1014548
2   < 10s   553552  566598
2   11 ~ 30 35440   82011
2   31~60   45558   124921
2   61~180  101529  390493
2   181~600 123027  820094
2   601~1800    98427   1137857
2   >1801   39178   1068057
3   < 10s   690598  706859
3   11 ~ 30 44409   102951
3   31~60   56585   156536
3   61~180  126382  492019
3   181~600 150267  1011472
3   601~1800    118928  1351807
3   >1801   45465   1195310
....

Now, I want to draw a dodged bar chart

现在,我想绘制一个躲闪的条形图

here is my code

这是我的代码

ggplot(data=mydata, aes(x=month,y=pageviews,fill=time_interval)) + geom_bar(stat="identity",position="dodge", colour="black")

Don't know why I got a weird graph as below :

不知道为什么我得到一个奇怪的图表如下:

奇怪的Y轴躲闪条形图

1 个解决方案

#1


1  

Your pageviews data is a factor. It will work if you transform it to a numeric variable. Furthermore, you shoudl reorder the levels of the factor time_interval:

您的综合浏览量数据是一个因素。如果将其转换为数字变量,它将起作用。此外,您需要重新排序因子time_interval的级别:

mydata <- transform(mydata, 
    time_interval = factor(time_interval, levels = 
        c('< 10s','11 ~ 30','31~60', '61~180','181~600', '601~1800','>1801')),
    pageviews = as.numeric(as.character(pageviews)))

The plot:

剧情:

library(ggplot2)
ggplot(data = mydata, aes(x = month, y = pageviews, fill = time_interval)) +  
    geom_bar(stat = "identity", position = "dodge", colour = "black")

奇怪的Y轴躲闪条形图

#1


1  

Your pageviews data is a factor. It will work if you transform it to a numeric variable. Furthermore, you shoudl reorder the levels of the factor time_interval:

您的综合浏览量数据是一个因素。如果将其转换为数字变量,它将起作用。此外,您需要重新排序因子time_interval的级别:

mydata <- transform(mydata, 
    time_interval = factor(time_interval, levels = 
        c('< 10s','11 ~ 30','31~60', '61~180','181~600', '601~1800','>1801')),
    pageviews = as.numeric(as.character(pageviews)))

The plot:

剧情:

library(ggplot2)
ggplot(data = mydata, aes(x = month, y = pageviews, fill = time_interval)) +  
    geom_bar(stat = "identity", position = "dodge", colour = "black")

奇怪的Y轴躲闪条形图