我有以下資料:
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()
這是輸出。

問題:
一種)。如何為圓圈添加顏色?我想洗掉顏色條并為圓圈著色。
b)。我也遇到了將顏色更改為 10 種離散顏色的問題。我想在這里使用 viridis() 。有關如何在 R 中正確執行此操作的任何建議?
viridis 顏色鏈接:https : //www.datanovia.com/en/blog/top-r-color-palettes-to-know-for-great-data-visualization/
我將不勝感激。
uj5u.com熱心網友回復:
要組合兩個比例的圖例,您必須做兩件事:
- 使用
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))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/350444.html
上一篇:使用gtopt_footnote_marks得到令人困惑的結果
下一篇:僅針對滿足條件的觀測值的列的均值
