ggplot2按形状分隔图例

# 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。发生这种情况时,我只想绘制“小”数字,因为不可能有比率。为此,我需要执行以下操作:

ratio.both <- Data %>% 
  filter(Large > 0) %>% 
  mutate(Ratio = Small/Large)

only.sml<- Data %>% 
  filter(Large < 1)

然后我将它们都绘制在同一张图上(按较长的数据):

ggplot() +
  geom_point(data = ratio.both,aes(x = Long,y = Lat,size = Ratio),stroke = 0,colour = '#3B3B3B',shape=16) +
  #
  geom_point(data = only.sml,size = Small,shape=1),stroke = 1,shape=1)

注意形状上的差异。这将绘制以下内容

ggplot2按形状分隔图例

不是最好的图,但演示了示例

比例(填充)的值和较小值的值之间的差在地图上清晰可见,但在图例中很难。

我想在图例中显示以下内容:

   #Title
   Size = both.ratio$Ratio,Shape/fill = Ratio or small value #whichever is easier
jiangzhiqinjiayou 回答:ggplot2按形状分隔图例

使用表中的变量通过内置的美学映射来对比数据要容易得多,而不是为小型和大型数据创建单独的几何。例如,您可以创建一个新变量来检查该数据点是属于大的还是小的“类型”。然后,您可以映射形状,颜色,大小或任何您想要的美观度,并可以选择手动添加比例(如果需要)。

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") 

enter image description here

或者如果要按点颜色进行对比:

Data %>% 
  mutate(is_large = ifelse(Large > 0,color = is_large)) +
  geom_point() +
  scale_color_manual(values = c("Ratio" = "blue","Small" = "red"),name = "Size") +
  scale_size_continuous(name = "Ratio/small value") 

enter image description here

本文链接:https://www.f2er.com/3079563.html

大家都在问