Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

ggplot2 seperating legend by shape

$
0
0
# 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))

I have a continuous variable, a ratio (small/large), I am successfully plotting.

Although, some 0s exist within the 'large' variable. When this occurs, I just want to plot the 'small' number as a ratio is impossible. To do this I have the following:

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

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

I then plot both on the same graph (by lat long data):

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)

Notice the difference in shape. This plots the following

not the nicest graph but demonstrates examplenot the nicest graph but demonstrates example

The difference between those which are a ratio (filled) and those which are just the small value is clear on the map but difficult in the legend.

I want the following in the legend:

   #Title
   Size = both.ratio$Ratio,
   Shape/fill = Ratio or small value #whichever is easier

Thanks Jim :)


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>