r - 如何在 R 中为 geom_point() 圆着色
问题描述
我有以下数据:
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))
我想使用 ggplot 绘制气泡图。我有以下脚本:
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()
这是输出。
问题:
一个)。如何为圆圈添加颜色?我想删除颜色条并为圆圈着色。
乙)。我也遇到了将颜色更改为 10 种离散颜色的问题。我想在这里使用 viridis() 。关于如何在 R 中正确执行此操作的任何建议?
viridis 颜色链接:https ://www.datanovia.com/en/blog/top-r-color-palettes-to-know-for-great-data-visualization/
我会很感激这方面的任何帮助。
解决方案
要结合两个比例的图例,您必须做两件事:
- 用作
guide_legend()
两个尺度的指南(已经默认为size
)。 - 确保所有
name
,breaks
,labels
,limits
等参数在刻度之间匹配。
要将 viridis 包中的离散颜色用于连续比例,您可以使用 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))
推荐阅读
- android - 三星设备上的 ACTION_IMAGE_CAPTURE 模糊
- php - Laravel 更新或创建关系
- ios - 如何使用 ReactiveUI 和 DynamicData 链接 SourceList 观察?
- android - 运动布局阻止更新 recyclerview
- wordpress - 如果apache以nobody运行:nobody,如何以特定用户:nobody的身份上传文件?
- php - 对于routes.php,laravel 5.2中不存在的一些随机网址,无法重定向到404
- powerbi - 从今天开始获取特定日期()
- angular - 刷新 ng2-smart-table
- amazon-web-services - 在 AWS Quicksight 中的星期一开始一周
- amazon-s3 - 将 Scrapy 爬虫与 S3 连接