我必须通过geom_text()来绘制表格,我尝试使用position_jitter / dodge和hjust使其看起来像一个表格,并改变文本重叠或以倾斜方式排列。
以下是示例代码:
require(ggplot2)
require(reshape2)
dia3 <- melt(CO2, id = c(colnames(CO2)[1],colnames(CO2)[2]))
dia3
p <- ggplot(dia3, aes_string(x=colnames(dia3)[2],y=colnames(dia3)[1],color = colnames(dia3)[3]))+
geom_text(aes_string(label = colnames(dia3)[4]),
position=position_dodge(width = 0.5),
hjust = 0.5,
size = 2.5
)+
scale_x_discrete(drop = TRUE)+
theme_bw()+
theme(
axis.ticks.x = element_blank(),
axis.text.y= element_text(color="black", size=8),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
legend.key = element_rect(fill="white"), legend.background = element_rect(fill=NA),
legend.position="bottom",
legend.title = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank()
)
p
我需要帮助安排所有文字以垂直直线排列。
由于
答案 0 :(得分:1)
躲避由group
完成,x
会自动设置为您已映射的任何因子变量,在这种情况下包括ggplot(dia3, aes_string(x = colnames(dia3)[2], y = colnames(dia3)[1],
color = colnames(dia3)[3], group = colnames(dia3)[3]))+
geom_text(aes_string(label = colnames(dia3)[4]),
hjust = 0.5,
size = 2.5,
position = position_dodge(0.5)) +
scale_x_discrete(drop = TRUE)
。覆盖默认值以解决问题:
<script src="https://cdnjs.cloudflare.com/ajax/libs/snap.js/1.9.3/snap.min.js"></script>