我需要使用 sec.axis 创建双 y 图,但无法使两个轴正确缩放。
我一直在遵循此线程中找到的说明:ggplot with 2 y axis on every side and differentscale
但每次我将 ylim.prim 中的下限更改为 0 以外的任何值时,都会弄乱整个图。出于可视化的原因,我需要两个轴的非常具体的 y 限制。另外,当我将 geom_col 更改为 geom_line 时,它也会弄乱辅助轴的限制。
climate <- tibble(
Month = 1:12,
Temp = c(23,23,24,24,24,23,23,23,23,23,23,23),
Precip = c(101,105,100,101,102, 112, 101, 121, 107, 114, 108, 120)
)
ylim.prim <- c(0, 125) # in this example, precipitation
ylim.sec <- c(15, 30) # in this example, temperature
b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])
ggplot(climate, aes(Month, Precip)) +
geom_col() +
geom_line(aes(y …Run Code Online (Sandbox Code Playgroud) 我用 ggplot 对箱线图进行了分组
require(ggplot2)
require(tidyr)
require(lubridate)
dat.1415<-as.data.frame(sample(1:1000, 181))
dat.1415$date<-seq(as.Date("2014-11-1"), as.Date("2015-4-30"), "day")
names(dat.1415)<-c("value", "date")
dat.1415$month<-month(dat.1415$date)
dat.1415$season<-"2014/15"
dat.1516<-as.data.frame(sample(1:1000, 182))
dat.1516$date<-seq(as.Date("2015-11-1"), as.Date("2016-4-30"), "day")
names(dat.1516)<-c("value", "date")
dat.1516$month<-month(dat.1516$date)
dat.1516$season<-"2015/16"
dat.1617<-as.data.frame(sample(1:1000, 181))
dat.1617$date<-seq(as.Date("2016-11-1"), as.Date("2017-4-30"), "day")
names(dat.1617)<-c("value", "date")
dat.1617$month<-month(dat.1617$date)
dat.1617$season<-"2016/17"
dat.1718<-as.data.frame(sample(1:1000, 181))
dat.1718$date<-seq(as.Date("2017-11-1"), as.Date("2018-4-30"), "day")
names(dat.1718)<-c("value", "date")
dat.1718$month<-month(dat.1718$date)
dat.1718$season<-"2017/18"
dat<-rbind(dat.1415, dat.1516, dat.1617, dat.1718)
dat$month<-month.abb[dat$month]
dat$month<-factor(dat$month)
dat$facet = factor(dat$month, levels = c("Nov", "Dec", "Jan", "Feb", "Mar", "Apr"))
ggplot(dat, aes(x=season, y=value)) +
geom_boxplot(fill="grey50") +
facet_grid(~facet) +
theme_classic()+
theme(legend.position="top") +
labs(x="", y="", title="") +
guides(fill=F) +
theme(panel.background = element_rect(fill="grey95"))
Run Code Online (Sandbox Code Playgroud)
但是因为它有太多的盒子,所以我在 …
我正在平滑时间序列数据并用 绘制它们ggplot。过去我使用 TTR 平滑数据,但最近开始在 ggplot 中动态平滑。然而,它产生了两件文物,我不确定我在这里错过了什么。
ggplot(data=df, aes(x=date, y=x, color=group))+
geom_line(aes(y=rollmean(x, 10, fill=NA, align='left'), color=group), na.rm= TRUE, size=0.75)
Run Code Online (Sandbox Code Playgroud)
产生
然而
df.1.ts<-read.zoo(df[df$group=='series1',], format = "%Y-%m-%d")
df.1.SMA10<-data.frame(apply(df.1.ts[,1,drop=F], 2, SMA, n=10))
df.1.SMA10<-cbind(as.Date(time(df.1.ts)), df.1.SMA10)
df.1.SMA10$group<-'series1'
names(df.1.SMA10)[1]<-'date'
df.2.ts<-read.zoo(df[df$group=='series2',], format = "%Y-%m-%d")
df.2.SMA10<-data.frame(apply(df.2.ts[,1,drop=F], 2, SMA, n=10))
df.2.SMA10<-cbind(as.Date(time(df.2.ts)), df.2.SMA10)
df.2.SMA10$group<-'series2'
names(df.2.SMA10)[1]<-'date'
df.SMA10<-rbind(df.1.SMA10, df.2.SMA10)
ggplot(data=df.SMA10, aes(x=date, y=x, color=group)) +
geom_line(size=0.75, na.rm=T)
Run Code Online (Sandbox Code Playgroud)
产生
样本数据:
df<-structure(list(date = structure(c(14242, 14243, 14244, 14245,
14246, 14247, 14248, 14249, 14250, 14251, 14252, 14253, 14254,
14255, 14256, 14257, 14258, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ggplot将热图绘制到地图上(效果很好)但是如果我限制轴,我的geom_polygon覆盖会以错误的方式连接点.
ggplot(2017.fixes, aes(x=Long, y=Lat) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour=F)+
scale_fill_distiller(palette="PuBu", direction=1) +
geom_polygon (data=map.df,aes(x=long, y=lat,group=group), color="grey50", fill="grey", na.rm=T) +
#xlim(-156.95, -156.4)+
#ylim(20.55, 21.05 )+
coord_fixed()
Run Code Online (Sandbox Code Playgroud)
产生这个:
但如果我取消注释xlim和ylim我得到这个:
它显然正确地切断了左岛,但不是其他两个,我不知道为什么.