R ggplot:将网格线与geom_平铺的末端对齐
我正在尝试使用R ggplot:将网格线与geom_平铺的末端对齐,r,ggplot2,R,Ggplot2,我正在尝试使用ggplotsgeom\u tile创建热图。当它站立时,网格线在每个geom\u瓷砖的中间居中。我想要的是网格线与每个平铺的开始/结束对齐。我看过一些相关的帖子(,但都是关于连续量表的。就我而言,两个量表都是离散的/因子。有什么想法吗?非常感谢 库(tidyverse) 我的虹膜% 变异(萼片间隔=切割(萼片长度,4))%>% 分组依据(萼片间隔,种类) 我的虹膜%>% 总结(n_obs=n())%>% ggplot()+ geom_瓷砖(aes(y=种类, x=萼片间隔, 填充
ggplots
geom\u tile
创建热图。当它站立时,网格线在每个geom\u瓷砖的中间居中。我想要的是网格线与每个平铺的开始/结束对齐。我看过一些相关的帖子(,但都是关于连续量表的。就我而言,两个量表都是离散的/因子。有什么想法吗?非常感谢
库(tidyverse)
我的虹膜%
变异(萼片间隔=切割(萼片长度,4))%>%
分组依据(萼片间隔,种类)
我的虹膜%>%
总结(n_obs=n())%>%
ggplot()+
geom_瓷砖(aes(y=种类,
x=萼片间隔,
填充=n_obs))+
主题_bw()+
主题(panel.grid=element\u line(color=“black”))
由(v0.3.0)于2020年1月28日创建,我相信您正在寻找以下方面的一些变化:
my_iris %>%
summarise(n_obs=n()) %>%
ggplot()+
geom_raster(aes(y=Species,
x=sepal_interval,
fill=n_obs),
hjust =0,
vjust =0)+
theme_bw()+
theme(panel.grid = element_line(color="black"))
我不相信hjust
和vjust
参数与geom_tile
一起工作,但是geom_graster
是帮助页面中使用的参数。hjust
和vjust
的值应该介于0和1之间,它们都默认为0.5,将它们集中在同一点。我使用取得了很多成功>geom_rect
在ggplot2
中制作热图。虽然前端有更多的数据处理,但我发现它更灵活,更容易定制
my_iris <- iris %>%
mutate(sepal_interval=cut(Sepal.Length, 4)) %>%
group_by(sepal_interval, Species) %>%
summarise(n_obs=n()) %>%
mutate(sepal.id = as.numeric(as.factor(sepal_interval)),
species.id = as.numeric(as.factor(Species)))
species.key <- levels(factor(my_iris$Species))
sepal.int.key <- levels(factor(my_iris$sepal_interval))
my_iris %>%
ggplot() +
geom_rect(aes(xmin = sepal.id, xmax = sepal.id+1, ymin = species.id, ymax = species.id+1, fill = n_obs)) +
theme_bw() +
scale_x_continuous(breaks = seq(1.5, length(sepal.int.key)+0.5, 1), labels = sepal.int.key, expand = c(0,0)) +
scale_y_continuous(breaks = seq(1.5, length(species.key) + 0.5, 1), labels = species.key, expand = c(0,0)) +
theme(panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.ontop = TRUE,
panel.background = element_rect(fill = "transparent"))
我的虹膜%
变异(萼片间隔=切割(萼片长度,4))%>%
分组依据(萼片间隔,种类)%>%
总结(n_obs=n())%>%
变异(sepal.id=as.numeric(as.factor(sepal_区间)),
species.id=as.numeric(as.factor(species)))
species.key将添加类似于以下内容的水平线和垂直线:帮助您?非常感谢。它非常接近,但不幸的是,据我所见,标签不是位于矩形的中间,而是位于分隔线/线条上。它似乎使用了主题(axis.text=element_text(hjust=0.5)etc有点帮助,但并不能完全解决问题。从您发布的链接来看,我假设您希望标签与每个磁贴的网格线对齐。不幸的是,没有简单的方法在离散比例上制作网格线,您需要手动添加连续比例