使用包ggplot2、dplyr和scales,我将代码绘制成下面的图
grafico_4 <- ggplot() +
ggtitle("Grafico variado") +
theme(plot.title = element_text(size = 10)) +
theme(panel.background = element_rect(fill='white', colour='white')) +
theme( axis.line = element_line(colour = "black", size = 0.5)) +
scale_y_discrete(limits = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")) +
scale_x_discrete(limits = c("uno", "", "tres", "", "cinco", "", "siete", "", "nueve", "")) +
geom_hline(yintercept = 5, linetype = "dotted") +
ylab("")
但是为什么我要在"uno“和"dos”之间,而不是在"cinco“和"siete","siete”和"nueve“之间,以及在"nueve”之后呢?我怎样才能让台词出现?
发布于 2021-10-23 21:04:07
scale_x_discrete
的极限参数是一个放置可能的标度值的地方。但它不能不止一次地显示相同的值。
因此,我能想到的最棘手的解决方案是使用不同的空白字符串:
scale_x_discrete(limits = c("uno", "", "tres", " ", "cinco", " ", "siete", " ", "nueve")) +
这是一个糟糕的解决方案,但我觉得有必要与大家分享。
编辑:这里有一种不同的方法,我认为它依赖于(在我看来)更典型的ggplot2语法。我没想到的一个问题是,ggplot2似乎不想打印x轴,即使指定了它的断点和限制,直到在"x-space“中存在一个geom --即geom_hline不会触发它,但在这种情况下,带有x值的不可见点会触发。
我认为在这里使用连续轴更自然。在本例中,我使用我在这里找到的一个技巧使x轴的标签在文本值和空白之间交替使用:https://stackoverflow.com/a/25961969/6851825,它将c("uno", "tres")
转换为c("uno", "", "tres", "")
。
nums <- c("uno", "tres", "cinco", "siete", "nueve")
ggplot() +
ggtitle("Grafico variado") +
theme_classic() +
theme(plot.title = element_text(size = 10)) +
scale_y_continuous(breaks = 1:10, name = NULL) +
scale_x_continuous(breaks = 1:10, name = NULL,
labels = c(rbind(nums, ""))) +
coord_cartesian(xlim = c(0,11), ylim = c(0,10), expand = 0) +
geom_hline(yintercept = 5, linetype = "dotted") +
annotate("point", x = 5, y = 0, alpha = 0)
https://stackoverflow.com/questions/69693962
复制