我在 R 中通過 Plotly 可視化了水平點圖。我的資料集包含 3 個數字變數和 1 個分類變數。
“原點”在 y 軸上。'change' 和 'rate' 變數被可視化成圓圈。現在我想把“百分比”變數放在圓圈的右軸上
df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
Percentage = c(23,16,32,71,3,60,15,21,44,60),
rate = c(10,12,20,200,-25,12,13,90,-105,23),
change = c(10,12,-5,12,6,8,0.5,-2,5,-2))
library(ggplot2)
ggplot(df, aes(x = rate, y = factor(origin, rev(origin))))
geom_hline(aes(yintercept = origin), color = 'gray')
geom_vline(xintercept = 0, linetype = 2, color = 'gray')
geom_point(aes(color = 'Rate'), size = 10)
geom_text(aes(label = rate), color = 'white')
geom_point(aes(x = change, color = 'Change'), size = 10)
geom_text(aes(label = change, x = change))
theme_minimal(base_size = 16)
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL)
scale_color_manual(values = c('#aac7c4', '#5f9299'))
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50'))
labs(color = NULL, y = NULL)
輸出:

預期輸出:

uj5u.com熱心網友回復:
你可以做
ggplot(df, aes(x = rate, y = factor(origin, rev(origin))))
geom_hline(aes(yintercept = origin), color = 'gray')
geom_vline(xintercept = 0, linetype = 2, color = 'gray')
geom_point(aes(color = 'Rate'), size = 10)
geom_text(aes(label = rate), color = 'white')
geom_point(aes(x = change, color = 'Change'), size = 10)
geom_text(aes(label = change, x = change))
geom_point(aes(x = 220, fill = "Percentage"), color = "blue",
size = 12, shape = 21)
geom_text(aes(x = 220, label = paste0(Percentage, "%")))
theme_minimal(base_size = 16)
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL)
scale_color_manual(values = c('#aac7c4', '#5f9299'))
scale_fill_manual(values = "white", name = NULL)
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50'))
coord_cartesian(xlim = c(-120, 230), ylim = c(0, 11),
expand = FALSE)
labs(color = NULL, y = NULL)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493104.html
