1

我正在尝试使用 geom_tile 创建热图,但并非所有坐标都有值。因此我的 geom_tile 看起来像这样: 在此处输入图像描述

创建图像的代码是

library(dplyr)
data(mpg)
mydata <- mpg %>% group_by(manufacturer, cyl) %>% summarize(count=n())

mydata %>% ggplot(aes(x=manufacturer, y=cyl, fill=count))+geom_tile()+geom_text(aes(label=count))
4

1 回答 1

2

用于complete填写缺失的序列:

library(tidyverse)

mpg %>%
  count(manufacturer, cyl, name = 'count')  %>%
  complete(manufacturer, cyl = seq(min(cyl), max(cyl)), 
           fill = list(count = 0)) %>%
  ggplot(aes(x=manufacturer, y=cyl, fill=count))+
  geom_tile()+ geom_text(aes(label=count))

在此处输入图像描述

于 2020-12-16T02:00:16.927 回答