使用包 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”之后得到界線?如何讓線條出現?
uj5u.com熱心網友回復:
的限制引數scale_x_discrete是放置可能的比例值的地方。但它不能多次顯示相同的值。
所以我能想到的最可能的解決方案是使用不同的空白字串:
scale_x_discrete(limits = c("uno", "", "tres", " ", "cinco", " ", "siete", " ", "nueve"))
這是一個糟糕的解決方案,但我覺得有必要分享。

編輯:這是一種不同的方法,我認為它依賴于(在我看來)更典型的 ggplot2 語法。我沒想到的一個問題是 ggplot2 似乎不想列印 x 軸,即使指定了它的中斷和限制,直到“x 空間”中存在一個 geom——即 geom_hline 贏了t 觸發它,但在這種情況下,具有 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)
uj5u.com熱心網友回復:
你所描述的不是限制;它們是 x 軸標簽。如果您對限制和標簽進行編碼,您將獲得預期的輸出。
library(ggplot2)
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 = factor(1:10),
labels = c("uno", "", "tres", "", "cinco", "", "siete", "", "nueve", "")
)
geom_hline(yintercept = 5, linetype = "dotted")
ylab("")

由reprex 包(v2.0.1)于 2021 年 10 月 24 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/334570.html
上一篇:從半小時到每小時的資料
