在多个 ggplots 之间设置共同的年份颜色?
Setting a common year color between multiple ggplots?
我有一个清理过的数据集(DropBox:[[[已删除 - 如果您需要工作示例的更新副本]]])2001 年至 2014 年之间的支出。我有三个图 - 一个箱线图和两个散点图- 我正在 grid.arrange
进入专栏进行比较。出于这个原因,我希望年份从上到下对齐,并且每一年的图表之间都有一种共同的颜色 - 再次用于比较。
到目前为止,这是我的产品(一切正常,只是不确定如何对齐映射)
require(ggplot2)
require(gridExtra)
require(ggthemes)
require(lubridate)
options(scipen=999)
date <- exp.s$Date
amount <- exp.s$Amount
N <- exp.s$total
xy <- data.frame(date, amount, N)
#boxplot, color by year of payment
b1<-ggplot(xy, aes(x = as.factor(year(date)), amount)) +
geom_boxplot(aes(fill = as.factor(exp.s$start_year))) +
scale_color_gdocs() +
theme_gdocs()
#jitter plot, color by year of payment
b2<-ggplot(xy, aes(x = as.factor(year(date)), amount)) +
geom_jitter(alpha=I(.8), aes(color=as.factor(year(date)), size = exp.s$tot)) +
scale_color_gdocs() + theme_gdocs()
#jitter, color by contract start year
b3<-ggplot(xy, aes(x = as.factor(year(date)), amount)) +
geom_jitter(alpha=I(.9), aes(color=as.factor(exp.s$start_year), size = exp.s$tot)) +
scale_color_gdocs() + theme_gdocs()
grid.arrange(b1, b2, b3, ncol=1)
试试下面的方法:
require(ggplot2)
require(gridExtra)
require(ggthemes)
require(lubridate)
options(scipen=999)
#read data
exp.s <- read.csv("2001_2014_Expenditures_Cleaned_Hashed.csv")
#make same levels for 2 variables
mylevels <- as.character(sort(unique(c(year(exp.s$Date),exp.s$start_year))))
#data prep
xy <- data.frame(
mydate = exp.s$Date,
mydateYYYY = factor(year(exp.s$Date),levels = mylevels),
start_year = factor(exp.s$start_year,levels = mylevels),
amount = exp.s$Amount,
N = exp.s$tot)
#boxplot, color by year of payment
b1<-ggplot(xy, aes(mydateYYYY, amount,
fill = start_year)) +
geom_boxplot() +
scale_fill_identity("legend") +
theme_classic() +
theme(axis.text.x=element_blank(),
axis.title.x=element_blank())
#jitter plot, color by year of payment
b2<-ggplot(xy, aes(mydateYYYY, amount,
alpha=0.8,color=mydateYYYY, size = N)) +
geom_jitter() +
scale_color_identity() +
theme_classic() +
theme(legend.position="none") +
theme(axis.text.x=element_blank(),
axis.title.x=element_blank())
#jitter, color by contract start year
b3<-ggplot(xy, aes(mydateYYYY, amount,
alpha=0.9, color=start_year, size = N)) +
geom_jitter() +
scale_color_identity() +
theme_classic() +
theme(legend.position="none")
grid.arrange(b1, b2, b3, ncol=1)
我有一个清理过的数据集(DropBox:[[[已删除 - 如果您需要工作示例的更新副本]]])2001 年至 2014 年之间的支出。我有三个图 - 一个箱线图和两个散点图- 我正在 grid.arrange
进入专栏进行比较。出于这个原因,我希望年份从上到下对齐,并且每一年的图表之间都有一种共同的颜色 - 再次用于比较。
到目前为止,这是我的产品(一切正常,只是不确定如何对齐映射)
require(ggplot2)
require(gridExtra)
require(ggthemes)
require(lubridate)
options(scipen=999)
date <- exp.s$Date
amount <- exp.s$Amount
N <- exp.s$total
xy <- data.frame(date, amount, N)
#boxplot, color by year of payment
b1<-ggplot(xy, aes(x = as.factor(year(date)), amount)) +
geom_boxplot(aes(fill = as.factor(exp.s$start_year))) +
scale_color_gdocs() +
theme_gdocs()
#jitter plot, color by year of payment
b2<-ggplot(xy, aes(x = as.factor(year(date)), amount)) +
geom_jitter(alpha=I(.8), aes(color=as.factor(year(date)), size = exp.s$tot)) +
scale_color_gdocs() + theme_gdocs()
#jitter, color by contract start year
b3<-ggplot(xy, aes(x = as.factor(year(date)), amount)) +
geom_jitter(alpha=I(.9), aes(color=as.factor(exp.s$start_year), size = exp.s$tot)) +
scale_color_gdocs() + theme_gdocs()
grid.arrange(b1, b2, b3, ncol=1)
试试下面的方法:
require(ggplot2)
require(gridExtra)
require(ggthemes)
require(lubridate)
options(scipen=999)
#read data
exp.s <- read.csv("2001_2014_Expenditures_Cleaned_Hashed.csv")
#make same levels for 2 variables
mylevels <- as.character(sort(unique(c(year(exp.s$Date),exp.s$start_year))))
#data prep
xy <- data.frame(
mydate = exp.s$Date,
mydateYYYY = factor(year(exp.s$Date),levels = mylevels),
start_year = factor(exp.s$start_year,levels = mylevels),
amount = exp.s$Amount,
N = exp.s$tot)
#boxplot, color by year of payment
b1<-ggplot(xy, aes(mydateYYYY, amount,
fill = start_year)) +
geom_boxplot() +
scale_fill_identity("legend") +
theme_classic() +
theme(axis.text.x=element_blank(),
axis.title.x=element_blank())
#jitter plot, color by year of payment
b2<-ggplot(xy, aes(mydateYYYY, amount,
alpha=0.8,color=mydateYYYY, size = N)) +
geom_jitter() +
scale_color_identity() +
theme_classic() +
theme(legend.position="none") +
theme(axis.text.x=element_blank(),
axis.title.x=element_blank())
#jitter, color by contract start year
b3<-ggplot(xy, aes(mydateYYYY, amount,
alpha=0.9, color=start_year, size = N)) +
geom_jitter() +
scale_color_identity() +
theme_classic() +
theme(legend.position="none")
grid.arrange(b1, b2, b3, ncol=1)