我有如下資料,我想簡單地添加一個圖例(藍色表示某事,紅色表示另一件事),出于某種原因,我什至沒有嘗試顯示:
我看過的一些帖子:
uj5u.com熱心網友回復:
如果你想有一個顏色的圖例,你必須把它映射到里面aes()而不是在它之后geom_col(aes())。之后,使用scale_color_identity.
dat %>%
as_tibble() %>%
ggplot(aes(x = group_size))
geom_col(aes(y = group_sum, color = "red"), position = "identity", fill = "transparent")
geom_label(
data = dat %>% distinct(group_size, .keep_all = TRUE),
mapping = aes(y = group_sum, label = group_sum), color = "red")
geom_col(aes(y = count * 10, color = "blue"), position = "identity", fill = "transparent")
geom_label(
data = dat %>% distinct(count, .keep_all = TRUE),
mapping = aes(y = count * 10, label = count), color = "blue")
scale_y_continuous(sec.axis = sec_axis(trans = ~ . / 10, name = "Count"))
labs(color = "Legend")
scale_color_identity(name = "Your Legend Name",
breaks = c("red", "blue"),
labels = c("Your red Name", "Your blue"),
guide = "legend")

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