我必須在 R 中使用 ggplot2 復制下圖

這是我的嘗試:
library(dplyr)
library(scales)
d <- data.frame(x=c(4.5,9,5.5), y=c(3.5,2.5,8))
f <- data.frame(x=c(9,10,5.5), y=c(2.5,2.5,8))
df <- data.frame(x1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) , y1 = c(1, 4, 2.5, 4.5, 5.5, 8.5, 2.5, 1, 1.5, 2.5), x2 = 1:10, y2 = 1:10, x3 = c(2.1875, 5.8125, 6.25, 8.125, 9.3125), y3 = c(7.5, 6.375, 3, 3.875, 2.1875), x4 = c(1.3125, 1.5, 1.6875, 3.6875, 7.25), y4 = c(7.1875, 9.9375, 9.0625, 9.375, 3.8125))
grafico_4 <- ggplot(df)
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_polygon(data = d, mapping=aes(x = x, y = y), color = "white", alpha = 3, fill = "#AED9E6")
geom_polygon(data = f, mapping=aes(x = x, y = y), color = "white", alpha = 3, fill = "#AED9E6")
geom_hline(yintercept = 5, linetype = "dotted")
xlab("")
ylab("")
geom_line(aes(x = x1, y = y1), size = 1, alpha = 0.9, linetype = 2)
geom_point(aes(x = x2, y = y2), shape = 1)
geom_point(aes(x = x3, y = y3), shape = 1, color = "red")
geom_point(aes(x = x4, y = y4), shape = 3, color = "green", size = 0.8)
annotate(geom = "text", x = 1.7, y = 7.5, label = "cinco", color = "purple", size = 3)
annotate(geom = "text", x = 2, y = 6, label = "dos", color = "yellow", size = 3)
annotate(geom = "text", x = 6.625, y = 9.125, label = "cuatro", color = "blue", size = 3)
annotate(geom = "text", x = 7.875, y = 2, label = "tres", color = "green", size = 3)
這給出了以下情節

現在,很明顯
- 我的虛線看起來很古怪
- 我的 x 軸應該更大
- 有一條白線穿過我的多邊形
如何修復我的圖表?
uj5u.com熱心網友回復:
- 更改
color="white"為color = "#AED9E6"在您的多邊形中應該處理白線,通過將邊緣繪制為與填充相同的顏色(或者,使用一組頂點而不是兩個三角形的并集來指定多邊形......) - 我不確定你認為虛線的“古怪”是什么
- 更改縱橫比和/或文本和功能的相對大小的最簡單方法是
ggsave()使用指定的高度和寬度。
uj5u.com熱心網友回復:
問題(對于第 1 項和第 2 項)不在于您對 ggplot 的使用,而在于未能按預期設定輸出設備。它是控制縱橫比的設備。虛線的抖動是由于您的設備解析度低(這可能是低解析度輸出計算機的默認螢屏設備)。您可以通過增加“像素密度”(dpi)來控制它。(我不需要增加png設備的默認值,因為它已經在創建更好的破折號再現,但如果需要,您可以使用幫助頁面中列出的引數對其進行修改:?png
png("test",width=700, height=480)
grafico_4
dev.off()

BB 已經描述了如何在交叉點處固定白線。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335779.html
上一篇:如何在ggplot2中制作雙y軸
