分类轴到连续ggplot

时间:2017-07-25 21:16:25

标签: r plot ggplot2 heatmap

我正在尝试创建一个热图,每个图块都是一个月。如果我使用日期作为数据日期类型,我会在图像上找到不可接受的行。

Heat Map with Data as Date

所以我的解决方案是将日期视为一个因素

Heat Map with Date as Factor

问题是轴不可见。有没有办法将两个图与第一个图中的x轴合并,但第二个图中的图块是什么?

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

作为一个可重复的例子:

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

1 个答案:

答案 0 :(得分:0)

使用日期但手动删除网格。你可以通过在情节中添加+ theme(panel.grid = element_blank())来实现。