我有兩個資料集,我想將它們組合成一個圖,一個作為條形圖(2 組),一個作為點線圖(1 組)。我已經成功地組合了這些圖并顯示了條形圖的圖例,但是每當我嘗試顯示點/線的圖例時,它都不起作用。
這是我的資料集的摘錄(已翻譯):
bargroup <- data.frame('Year' = as.numeric(rep(2001:2004, each = 2)),
'Group' = as.factor(rep(c('State', 'Country'), 4)),
'Share' = as.numeric(c(0.42, 0.41, 0.4, 0.4, 0.42, 0.4, 0.42, 0.38)))
plgroup <- data.frame('Year' = as.numeric(2001:2004),
'Group' = as.factor(rep('State', 4)),
'Value' = as.numeric(c(4.95, 5.31, 5.29, 4.96)))
這是我當前的代碼:
ggplot()
geom_bar(data = bargroup, aes(x = Year, y = Share, fill = Group, group = Group),
stat = 'identity', position = position_dodge2(preserve = 'single'))
geom_point(data = plgroup, aes(y = Value*0.1, x = Year), size = 4, color = '#875DA3')
geom_line(data = plgroup, aes(y = Value*0.1, x = Year), size = 1, color = '#875DA3')
labs(x = 'Year')
scale_y_continuous(name = 'Share groups', labels = scales::percent,
sec.axis = sec_axis(~.*10, name = 'Cost'))
scale_fill_manual(labels = c('Share State', 'Share Country'),
values = c('#659B7A', '#8CD7F0'))
scale_color_manual(labels = c('Total Cost'),
values = c('#875DA3'))
theme_minimal()
theme(legend.title = element_blank(),
legend.position = 'bottom',
plot.title = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 18),
axis.title.y = element_text(size = 18),
axis.text = element_text(size = 16),
legend.text = element_text(size = 18))
guides(fill = guide_legend(nrow = 2, byrow = T))
dev.off()
這是我得到的圖表:

如您所見,點/線組未顯示在圖例中。然后我嘗試洗掉引數 scale_color_manual() 并調整 geom_point() 和 geom_line() 引數,如下所示:
geom_point(data = plgroup, aes(y = Value*0.1, x = Year, color = Group),
size = 4, color = '#875DA3', show.legend = T)
geom_line(data = plgroup, aes(y = Value*0.1, x = Year, color = Group),
size = 1, color = '#875DA3', show.legend = T)
我從中得到的圖表是:

當我使用 melt() 函陣列合兩個資料集時,我得到了相同的圖表。
我還嘗試從中制作 3 個組而不顯示點/線組。這顯示了所有 3 個組,但不幸的是,點/線組現在 - 顯然 - 用正方形而不是其點/線符號(在圖例中)標記。
有誰知道如何調整我的代碼以將點/線組顯示為其他兩組旁邊或下方的單個專案符號?最好還可以選擇標簽名稱(“總成本”)。
非常感謝您!
uj5u.com熱心網友回復:
經驗法則:里面的一切aes()都會產生一個傳奇。所以size把aes() AND color放入aes():
ggplot()
geom_bar(data = bargroup, aes(x = Year, y = Share, fill = Group, group = Group),
stat = 'identity', position = position_dodge2(preserve = 'single'))
geom_point(data = plgroup, aes(y = Value*0.1, x = Year, color = '#875DA3'),size = 4)
geom_line(data = plgroup, aes(y = Value*0.1, x = Year, color = '#875DA3'),size = 1)
labs(x = 'Year')
scale_y_continuous(name = 'Share groups', labels = scales::percent,
sec.axis = sec_axis(~.*10, name = 'Cost'))
scale_fill_manual(labels = c('Share State', 'Share Country'),
values = c('#659B7A', '#8CD7F0'))
scale_color_manual(labels = c('Total Cost'),
values = c('#875DA3'))
theme_minimal()
theme(legend.title = element_blank(),
legend.position = 'bottom',
plot.title = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 18),
axis.title.y = element_text(size = 18),
axis.text = element_text(size = 16),
legend.text = element_text(size = 18))
guides(fill = guide_legend(nrow = 2, byrow = T))

改變圖例的順序:
guides(fill = guide_legend(nrow = 2, byrow = T, order=1))

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