使用expand = c(0,0)时,沿x轴的标签消失 [英] Labels along x axis disappear when using expand=c(0, 0)

查看:525
本文介绍了使用expand = c(0,0)时,沿x轴的标签消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a question regarding to the labels of the x-axis. Assume I've the following plot:

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)) 

具有以下格式:

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

How can I change the labels of the x-axis to (1,2,3,4,5,6,7,8,9,10) while using expand=c(0,0) for x ? If I'm using

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")

推荐答案

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

You're plotting continuous data along the x axis, so the correct scale is scale_x_continuous(). The reason the labels are disappearing is because you're erroneously using 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

我没有您的变量long_form_textq$value,所以我改用了long_form_q$value.请注意,通过aes()函数将数据馈送到ggplot几乎总是一个坏主意.数据应通过data =参数提供.

I didn't have your variable long_form_textq$value, so I used long_form_q$value instead. Note that it is almost always a bad idea to feed data into ggplot via the aes() function. Data should be provided via the data = argument.

这篇关于使用expand = c(0,0)时,沿x轴的标签消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆