使用expand = c(0,0)时,沿x轴的标签消失

时间:2018-04-25 21:12:29

标签: r ggplot2 graphic

我对x轴的标签有疑问。假设我有以下情节:

p <- ggplot(long_form_q, aes(reihe, variable)) + geom_tile(aes(fill = value), colour = "white") 

 pneu <- p + scale_fill_gradient(low = "white",high = "steelblue", limits= c(1,3), breaks=c(1,2,3)) + 
 geom_text(aes(label=long_form_textq$value)) +
 theme(axis.title.x = element_blank(),axis.title.y =element_blank())  +
 theme(axis.text.y = element_text(size=18, color = "black"), axis.text.x = element_text(size=14)) +
scale_y_discrete(labels=c(h_3x3.1="3x3", h_3x5.1="3x5", h_3x9.1 ="3x9"), expand=c(0,0)) 

以下表格:

enter image description here

对x使用expand = c(0,0)时,如何将x轴的标签更改为(1,2,3,4,5,6,7,8,9,10)?如果我正在使用

scale_x_discrete(expand=c(0,0))

标签消失

我的dput是:

dput(long_form_q)
structure(list(reihe = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L), variable = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("h_3x3.1", 
"h_3x5.1", "h_3x9.1"), class = "factor"), value = c(1, 1, 1, 
2, 1, 1, 1, 1, 1, 1, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 1, 
3, 3, 3, 3, 3, 3)), row.names = c(NA, -30L), .Names = c("reihe", 
"variable", "value"), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

您正在沿x轴绘制连续数据,因此正确的比例为scale_x_continuous()。标签消失的原因是您错误地使用scale_x_discrete()

pneu <- p + scale_fill_gradient(low = "white",high = "steelblue", limits= c(1,3), breaks=c(1,2,3)) + 
  geom_text(aes(label=value)) +
  theme(axis.title.x = element_blank(),axis.title.y =element_blank())  +
  theme(axis.text.y = element_text(size=18, color = "black"), axis.text.x = element_text(size=14)) +
  scale_y_discrete(labels=c(h_3x3.1="3x3", h_3x5.1="3x5", h_3x9.1 ="3x9"),
                   expand=c(0, 0)) + 
  scale_x_continuous(expand = c(0, 0), breaks = 1:10)

pneu

enter image description here

我没有您的变量long_form_textq$value,因此我使用long_form_q$value代替。请注意,通过aes()函数将数据输入ggplot几乎总是一个坏主意。数据应通过data =参数提供。