你如何在ggplot中订购月份 [英] how do you order Months in ggplot

查看:105
本文介绍了你如何在ggplot中订购月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据框叫yy:

  structure(list(Time = structure(c(1209096000,1238731200,1272600000 
1301666400,1335794400,1364835600,1218772800,1250222400,1280808000 $ b $ 1314028800,1346421600,1377835200,1229317200,1262235600 1291352400
1324047600 1355497200 1385960400 1204261200 1235710800 1265950800
1298646000,1328281200,1360940400,1199250000,1232082000,1263186000
1295017200,1326466800,1357149600),class = c(POSIXct,POSIXt
),tzone =),Peak_Logons = c(472452L,398061L,655849L,881689L,
873720L,1278295L,340464L,520943L,1995150L,883184L,931721L,
1098553L,405193L,638301L,734635L,1254951L,962391L,1126432L,
316200L,477407L,674884L,812793L,898550L,1541478L,291564L,
394967L,902076L,916832L,878264L,918102L) 2011,2012,2013​​,2008,2009,2010,
2011,2012,2013​​,2008,2009, ,2 011,2012,
2013,2008,2009,2010,2011,2012,2013,2008,
2009 ,2010,2011,2012,2013​​),Month = c(April,April,
April,April,April,April 八月,八月,八月,
八月,八月,八月,十二月,十二月,十二月,
十二月, ,十二月,二月,二月,二月,
二月,二月,二月,一月,一月 1月,1月,1月)),.Names = c(Time,Peak_Logons,
Year,Month),row.names = c(35479L,30535L, 23645L,15248L,
49696L,8077L,24651L,13098L,20204L,47450L,41228L,20740L,
28049L,9739L,2636L,50230L,43746L,3435L,38091L,28351L,
7382L, 3343L,47824L,45481L,23951L,29664L,10024L,4545L,
38808L,44205L),class =data.frame)

我想要做的是创建一个热图,y轴上的年份和x轴上的月份。



我这样做:

  ggplot(yy,aes( ()= + 
geom_tile()+
theme_bw()+
guides(fill = guide_legend(keywidth = 5,keyheight = 1))+
主题(axis.text.x = element_text(size = 10,angle = 45,hjust = 1))

这种方法可行,但x轴上的月份不是一月,二月,三月,四月......十二月的顺序。

从4月,8月开始,等等。


  1. 如何订购1月至12月的x轴?

  2. 一种改变默认颜色的方式,它看起来像是使用蓝色阴影?

  3. 我可以使用geom_tiles文本吗?我想在瓦片中插入Time和Peak_Logons。

我非常感谢任何见解。

解决方案


  • 使用reorder排列轴标签。我创建了一个包含月份索引的新列。

  • geom_text 添加文本。也许你应该玩文字大小。

  • scale_fill_gradientn 可以改变填充颜色。另见 scale_fill_gradientn




  dat.m<  - 数据.frame(Month = months(seq(as.Date(2000/1/1),
by =month,length.out = 12)),month.id = 1:12)

yy< - merge(yy,dat.m)
library(ggplot2)
ggplot(yy,aes(reorder(Month,month.id),Year,fill = Peak_Logons ))+
geom_tile()+
theme_bw()+
guides(fill = guide_legend(keywidth = 5,keyheight = 1))+
theme(axis.text.x =元素文本(大小= 10,角度= 45,hjust = 1))+
geom_text(aes(label = paste(Peak_Logons,format(Time,%H),sep =' - ')))+
scale_fill_gradient(low =yellow,high =red)


I have this data frame called yy:

structure(list(Time = structure(c(1209096000, 1238731200, 1272600000, 
1301666400, 1335794400, 1364835600, 1218772800, 1250222400, 1280808000, 
1314028800, 1346421600, 1377835200, 1229317200, 1262235600, 1291352400, 
1324047600, 1355497200, 1385960400, 1204261200, 1235710800, 1265950800, 
1298646000, 1328281200, 1360940400, 1199250000, 1232082000, 1263186000, 
1295017200, 1326466800, 1357149600), class = c("POSIXct", "POSIXt"
), tzone = ""), Peak_Logons = c(472452L, 398061L, 655849L, 881689L, 
873720L, 1278295L, 340464L, 520943L, 1995150L, 883184L, 931721L, 
1098553L, 405193L, 638301L, 734635L, 1254951L, 962391L, 1126432L, 
316200L, 477407L, 674884L, 812793L, 898550L, 1541478L, 291564L, 
394967L, 902076L, 916832L, 878264L, 918102L), Year = c("2008", 
"2009", "2010", "2011", "2012", "2013", "2008", "2009", "2010", 
"2011", "2012", "2013", "2008", "2009", "2010", "2011", "2012", 
"2013", "2008", "2009", "2010", "2011", "2012", "2013", "2008", 
"2009", "2010", "2011", "2012", "2013"), Month = c("April", "April", 
"April", "April", "April", "April", "August", "August", "August", 
"August", "August", "August", "December", "December", "December", 
"December", "December", "December", "February", "February", "February", 
"February", "February", "February", "January", "January", "January", 
"January", "January", "January")), .Names = c("Time", "Peak_Logons", 
"Year", "Month"), row.names = c(35479L, 30535L, 23645L, 15248L, 
49696L, 8077L, 24651L, 13098L, 20204L, 47450L, 41228L, 20740L, 
28049L, 9739L, 2636L, 50230L, 43746L, 3435L, 38091L, 28351L, 
7382L, 3343L, 47824L, 45481L, 23951L, 29664L, 10024L, 4545L, 
38808L, 44205L), class = "data.frame")

What I would like to do is create a heat map, Year on the y-axis and Month on the x-axis.

I am doing this:

 ggplot(yy ,aes(Month, Year, fill=Peak_Logons)) + 
   geom_tile() + 
   theme_bw() + 
   guides(fill = guide_legend(keywidth = 5, keyheight = 1)) + 
   theme(axis.text.x = element_text(size=10, angle=45, hjust=1))

This kinda works but Months on x-axis are not order from January, February, March, April ... December.

They are order aphabetically from April, August etc.

  1. How would I order the x-axis from January to December?
  2. Is there a way to change the default colors, it looks like it is using blue shades?
  3. Can I isert text the the geom_tiles? I would like to insert Time and Peak_Logons inside the tiles.

I would really appreciate any insight.

解决方案

  • Use reorder to arrange your axis labels. I create a new column with month index.
  • geom_text to add text. Maybe you should play with text size.
  • scale_fill_gradientn to change fill color. See also scale_fill_gradientn

dat.m <- data.frame(Month=months(seq(as.Date("2000/1/1"), 
              by = "month", length.out = 12)),month.id = 1:12)

yy <- merge(yy,dat.m)
library(ggplot2)
ggplot(yy ,aes(reorder(Month,month.id), Year, fill=Peak_Logons)) +
    geom_tile() + 
    theme_bw() + 
    guides(fill = guide_legend(keywidth = 5, keyheight = 1)) + 
    theme(axis.text.x = element_text(size=10, angle=45, hjust=1)) +
    geom_text(aes(label=paste(Peak_Logons,format(Time,"%H"),sep='-'))) +
    scale_fill_gradient(low = "yellow",  high = "red")

这篇关于你如何在ggplot中订购月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆