代码之家  ›  专栏  ›  技术社区  ›  LLL

ggplot2中的单级变量::geom\u tile

  •  0
  • LLL  · 技术社区  · 9 年前

    我有一个包含三个变量的数据框, location , price 和 varname .

    我想使用ggplot2 geom_tile 制作各种各样的热图。这个图看起来几乎像一个条形图,但我更喜欢 geom_瓷砖 因为我喜欢在绘图上分配相同数量的物理空间,无论大小。我的代码几乎把我带到了那里。

    第一个问题是我无法格式化绘图,因此无法去掉伪条左右两侧的所有空白。第二个问题是我无法删除 Price 下面的故事情节,因为我想 价格 仅在绘图上方的图例中显示。

    谢谢你的帮助!

    起点( df ):

    df <- data.frame(location=c("AZ","MO","ID","MI"),price=c(1380.45677,1745.1245,12.45652,1630.65341),varname=c("price","price","price","price"))
    

    当前代码:

    library(ggplot2)
    ggplot(df, aes(varname,location, width=.2)) + geom_tile(aes(fill = price),colour = "white") + geom_text(aes(label = round(price, 3))) +
      scale_fill_gradient(low = "ivory1", high = "green") +
      theme_classic() + labs(x = "", y = "") + theme(legend.position = "none") + ggtitle("Price")
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Axeman    9 年前
    1. 不要将宽度设置为0.2。
    2. 使用 theme 禁用标签和记号。
    3. 您可能想要使用 coord_equal 以获得良好的比例(即正方形)。 expand = FALSE 清除所有空白。

    .

    ggplot(df, aes(varname, location)) + 
        geom_tile(aes(fill = price), colour = "white") + 
        geom_text(aes(label = round(price, 3))) +
        scale_fill_gradient(low = "ivory1", high = "green") +
        theme_classic() + labs(x = "", y = "") + 
        theme(legend.position = "none", axis.text.x = element_blank(), axis.ticks.x = element_blank()) + 
        ggtitle("Price") + 
        coord_equal(expand = FALSE)
    

    enter image description here

    推荐文章