Home > OS >  How to color geom_point() circle in R
How to color geom_point() circle in R

Time:11-06

I have the following data:

dat <- structure(list(station = c(980009L, 980042L, 980145L, 980338L, 
980351L, 980365L, 980563L), JAN = c(0.04, 0.04, 0.04, 0.04, 0.04, 
0.05, 0), FEB = c(0.05, 0.04, 0.99, 0.99, 0.99, 0.04, 0.99), 
 MAR = c(0.04, 0.99, 0.99, 0.99, 0.99, 0.99, 0), APR = c(0.05, 
 0.04, 0.99, 0.99, 0.99, 0.99, 0), MAY = c(0.06, 0.05, 0.95, 
 0.05, 0.96, 0.94, 0.98), JUN = c(0.05, 0.07, 0.04, 0.04, 
 0.05, 0.04, 0.99), JUL = c(0.05, 0.04, 0, 0.93, 0.92, 0.97, 
 0), AUG = c(0.04, 0.06, 0.98, 0.98, 0.99, 0.99, 0), SEP = c(0.04, 
 0.04, 0.98, 0.04, 0.04, 0.04, 0), OCT = c(0.04, 0.07, 0.99, 
 0.05, 0.04, 0.99, 0), NOV = c(0.04, 0.07, 0.99, 0.05, 0.06, 
 0.05, 0), DEC = c(0.04, 0.99, 0.05, 0.04, 0.04, 0.05, 0)), class = "data.frame", 
 row.names = c(NA,-7L))

I want to plot a bubble plot using ggplot. I have the following script:

library(ggplot2)
library(reshape2)
dat<-read.csv("dat.csv",header=T)
dat<-as.data.frame(dat)
pcm<-melt(dat,id = c("station"))

pcm$station<-factor(pcm$station,levels=unique(pcm$station))

p<-ggplot(pcm, aes(x = variable, y = station))
p<-p   geom_point(aes(fill=value*100,size=value*100),alpha = 0.5, shape = 21)
p<-p   coord_fixed(ratio=1)
p<-p   labs( x= "month", y = "station", size = "Percentage", fill ="")

p<-p   scale_size_continuous(limits=c(0,100),breaks=seq(0,100,by=10))
p<-p   scale_x_discrete(expand=c(0.1,0))



png("Bubble_plot.png")
print(p)
dev.off()

Here's the output.

enter image description here

Questions:

a). How can I add colors to the circles? I want to remove the color bar and color the circles.

b). I am also having a problem changing the color into 10 discrete colors. I want to use viridis() here. Any suggestions on how to do this correctly in R?

link to viridis color: https://www.datanovia.com/en/blog/top-r-color-palettes-to-know-for-great-data-visualization/

I'll appreciate any help on this.

CodePudding user response:

To combine legends for two scales you must do two things:

  1. Use guide_legend() as the guide for both scales (already default for size).
  2. Ensure all name, breaks, labels, limits, etc. parameters match between the scales.

To use discrete colours from the viridis package for a continuous scale you can use the binned colour/fill scale scale_{colour/fill}_viridis_b().

# dat <- structure(...) # omitted for brevity
  
library(ggplot2)
library(reshape2)
dat<-as.data.frame(dat)
pcm<-melt(dat,id = c("station"))

pcm$station<-factor(pcm$station,levels=unique(pcm$station))

ggplot(pcm, aes(x = variable, y = station))   
  geom_point(aes(fill=value*100,size=value*100),alpha = 0.5, shape = 21)   
  coord_fixed(ratio=1)  
  labs( x= "month", y = "station", size = "Percentage", fill ="Percentage")  
  scale_size_continuous(limits=c(0,100),breaks=seq(0,100,by=10))  
  scale_fill_viridis_b(limits = c(0, 100), breaks = seq(0, 100, by = 10),
                        guide = guide_legend())  
  scale_x_discrete(expand=c(0.1,0))

  • Related