[英]ggplot2 separating legend by shape
# Data:
zz <- "Small Large Lat Long
1 51 2 11 10
2 49 0 12 11
3 77 7 13 13
4 46 5 12 15
5 32 6 13 14
6 54 3 15 17
7 68 0 14 10
8 39 5 12 13"
Data <- as.data.frame(read.table(text=zz, header = TRUE))
我有一個連續變量,一個比率(小/大),我正在成功繪制。
雖然,“大”變量中存在一些 0。 發生這種情況時,我只想將 plot 的“小”數作為比例是不可能的。 為此,我有以下內容:
ratio.both <- Data %>%
filter(Large > 0) %>%
mutate(Ratio = Small/Large)
only.sml<- Data %>%
filter(Large < 1)
然后我 plot 都在同一張圖上(按經緯度數據):
ggplot() +
geom_point(data = ratio.both,
aes(x = Long,
y = Lat,
size = Ratio),
stroke = 0,
colour = '#3B3B3B',
shape=16) +
#
geom_point(data = only.sml,
aes(x = Long,
y = Lat,
size = Small,
shape=1),
stroke = 1,
shape=1)
注意形狀的不同。 這繪制了以下內容
map 上的比率(填充)和那些只是小值之間的區別很明顯,但在傳說中很難。
我想要傳說中的以下內容:
#Title
Size = both.ratio$Ratio,
Shape/fill = Ratio or small value #whichever is easier
使用內置的美學映射在表格中使用變量來對比數據要容易得多,而不是為小數據和大數據創建單獨的幾何圖形。 例如,您可以創建一個新變量來檢查該數據點是屬於大“類型”還是屬於小“類型”。 然后,您可以 map 形狀、顏色、大小或任何您想要的美學,並可選擇手動為這些添加比例(如果需要)。
Data %>%
mutate(is_large = ifelse(Large > 0, "Ratio", "Small"),
size = ifelse(is_large == "Large", Small/Large, Small)) %>%
ggplot(aes(Long, Lat,
size = size,
shape = is_large)) +
geom_point() +
scale_shape_manual(values = c("Ratio" = 16, "Small" = 1),
name = "Size") +
scale_size_continuous(name = "Ratio/small value")
或者,如果您想按點顏色進行對比:
Data %>%
mutate(is_large = ifelse(Large > 0, "Ratio", "Small"),
size = ifelse(is_large == "Large", Small/Large, Small)) %>%
ggplot(aes(Long, Lat,
size = size,
color = is_large)) +
geom_point() +
scale_color_manual(values = c("Ratio" = "blue", "Small" = "red"),
name = "Size") +
scale_size_continuous(name = "Ratio/small value")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.