简体   繁体   English

如何在 R 中为 geom_point() 圆圈着色

[英]How to color geom_point() circle in R

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.我想使用 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.这是输出。

在此处输入图片说明

Questions:问题:

a).一种)。 How can I add colors to the circles?如何为圆圈添加颜色? I want to remove the color bar and color the circles.我想删除颜色条并为圆圈着色。

b). b)。 I am also having a problem changing the color into 10 discrete colors.我也遇到了将颜色更改为 10 种离散颜色的问题。 I want to use viridis() here.我想在这里使用 viridis() 。 Any suggestions on how to do this correctly in R?有关如何在 R 中正确执行此操作的任何建议?

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

I'll appreciate any help on this.我将不胜感激。

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 ).使用guide_legend()作为两个比例的指南(已经是size默认值)。
  2. Ensure all name , breaks , labels , limits , etc. parameters match between the scales.确保所有的namebreakslabelslimits等参数在尺度之间匹配。

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() .要将 viridis 包中的离散颜色用于连续比例,您可以使用分箱颜色/填充比例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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM