简体   繁体   English

分类轴到连续ggplot

[英]Categorical axis to Continuous ggplot

I am trying to create a heat map, where each tile is a month. 我正在尝试创建一个热图,每个图块是一个月。 If I use the dates as a data date type, I get lines over my image that are not acceptable. 如果使用日期作为数据日期类型,则会在图像上出现不可接受的线条。

以数据为日期的热图

So my solution has been to to treat the date as a factor 所以我的解决方案是将日期视为一个因素

以日期为因子的热图

The problem is that the axis is not presentable. 问题在于轴不可显示。 Is there a way to merge the two plots with the x axis from the first plot, but the tiles from the second plot? 有没有一种方法可以将两个图与第一个图的x轴合并,但是将第二个图的图块合并?

ggplot(df, aes(x    = factor(Var2),
           y    = factor(desc(Var1)),
           fill = value)) +

As a reproducible example: 作为可重现的示例:

df <- read.table("","Var1","Var2","value","Col"
                 "1",2001-01-31,2001-01-31,-0.0118511587908436,"blue"
                 "2",2001-02-28,2001-01-31,0,"white"
                 "3",2001-03-30,2001-01-31,0,"white"
                 "4",2001-04-30,2001-01-31,0,"white"
                 "5",2001-05-31,2001-01-31,0,"white"
                 "6",2001-06-29,2001-01-31,0,"white")

ggplot(dff, aes(x   = factor(Var2),
           y    = factor(desc(Var1)),
           fill = Col)) +
  geom_tile() +
  scale_x_discrete(breaks=pretty(p$Var2)) 

Output from example 示例输出

Use dates but manually remove the grid. 使用日期,但手动删除网格。 You can do it by adding + theme(panel.grid = element_blank()) to the plot. 您可以通过在绘图中添加+ theme(panel.grid = element_blank())来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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