使用 expand=c(0, 0) 时沿 x 轴的标签消失
Labels along x axis disappear when using expand=c(0, 0)
我有一个关于 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))
以下形式:
如何在对 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")
您正在沿 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
我没有你的变量 long_form_textq$value
,所以我用 long_form_q$value
代替。请注意,通过 aes()
函数将数据输入 ggplot 几乎总是一个坏主意。应通过 data =
参数提供数据。
我有一个关于 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))
以下形式:
如何在对 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")
您正在沿 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
我没有你的变量 long_form_textq$value
,所以我用 long_form_q$value
代替。请注意,通过 aes()
函数将数据输入 ggplot 几乎总是一个坏主意。应通过 data =
参数提供数据。