ggplot2学习(5)
2019-11-21 本文已影响0人
BioLearner
> library(ggplot2)
> library(gcookbook)
> BOD
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6 #数据Time中没有6
6 7 19.8
> ggplot(BOD, aes(Time, demand)) + geom_line()
> BOD1 <- BOD #Make a copy of the data
> BOD1$Time <- factor(BOD1$Time)
> ggplot(BOD1, aes(Time, demand)) + geom_line()
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
# 因为此时Time为因子型,会有多个分组,如果不加group = 1,使用geom_line()会报错
> ggplot(BOD1, aes(Time, demand, group = 1)) + geom_line()
# 如果是使用 geom_point() 则没问题
ggplot(BOD1, aes(Time, demand)) + geom_point()
# 限定y轴范围
ggplot(BOD, aes(Time, demand)) + geom_line() +
ylim(0, max(BOD$demand))
# Adding point to a line graph
ggplot(BOD, aes(Time, demand)) + geom_line() +
geom_point(shape = 1, size = 3, aes(colour = factor(Time)))
> # Making a line graph with multiple lines
> # set a data
> library(plyr)
> tg <- ddply(ToothGrowth, c("supp","dose"),
summarise, length = mean(len))
> tg
supp dose length
1 OJ 0.5 13.23
2 OJ 1.0 22.70
3 OJ 2.0 26.06
4 VC 0.5 7.98
5 VC 1.0 16.77
6 VC 2.0 26.14
> ggplot(tg, aes(dose, length, colour = supp)) + geom_line()
ggplot(tg, aes(dose, length, linetype = supp)) + geom_line()
ggplot(tg, aes(dose, length)) + geom_line()
ggplot(tg, aes(dose, length, fill = supp)) + geom_line() +
geom_point(size = 4, shape = 21)
# Changing the appearance of lines
ggplot(BOD, aes(Time, demand)) +
geom_line(linetype = "dashed", size =1, colour = "blue")
# Making a graph with a shaded area 即将线图下面区域进行填充
# set a data
sunspotyear <- data.frame(
Year = as.numeric(time(sunspot.year)),
Sunspots = as.numeric(sunspot.year)
)
ggplot(sunspotyear, aes(Year, Sunspots)) + geom_line()
ggplot(sunspotyear, aes(Year, Sunspots)) + geom_area()
ggplot(sunspotyear, aes(Year, Sunspots)) +
geom_area(colour = "black", fill = "blue", alpha = 0.2)
> # Making a stacked area graph
> str(uspopage)
'data.frame': 824 obs. of 3 variables:
$ Year : int 1900 1900 1900 1900 1900 1900 1900 1900 1901 1901 ...
$ AgeGroup : Factor w/ 8 levels "<5","5-14","15-24",..: 1 2 3 4 5 6 7 8 1 2 ...
$ Thousands: int 9181 16966 14951 12161 9273 6437 4026 3099 9336 17158 ...
> ggplot(uspopage, aes(Year, Thousands)) + geom_area()
ggplot(uspopage, aes(Year, Thousands, fill = AgeGroup)) + geom_area()
ggplot(uspopage, aes(Year, Thousands, fill = AgeGroup)) +
geom_area(colour = "black", size = 0.2, alpha = 0.4)
ggplot(uspopage, aes(Year, Thousands, fill = AgeGroup, order = desc(AgeGroup))) +
geom_area(colour = "NA", alpha = 0.4) +
geom_line(position = "stack", size = 0.2)
# colour = "NA" 图例的区别
> # Making a proportional stacked area graph
> uspopage_prop <- ddply(uspopage, "Year", transform,
Percent = Thousands/sum(Thousands)*100)
> str(uspopage_prop)
'data.frame': 824 obs. of 4 variables:
$ Year : int 1900 1900 1900 1900 1900 1900 1900 1900 1901 1901 ...
$ AgeGroup : Factor w/ 8 levels "<5","5-14","15-24",..: 1 2 3 4 5 6 7 8 1 2 ...
$ Thousands: int 9181 16966 14951 12161 9273 6437 4026 3099 9336 17158 ...
$ Percent : num 12.1 22.3 19.6 16 12.2 ...
> ggplot(uspopage_prop, aes(Year, Percent, fill = AgeGroup)) +
geom_area(colour = "black", size = .2, alpha = .4)
ggplot(uspopage_prop, aes(Year, Percent, fill = AgeGroup)) +
geom_area(colour = NA, size = .2, alpha = .4) +
geom_line()
ggplot(uspopage_prop, aes(Year, Percent, fill = AgeGroup)) +
geom_area(colour = NA, size = .2, alpha = .4) +
geom_line(position = "stack", size = .2)
> # Adding a confidece region 即上下变动的区间
> clim <- subset(climate, source = "Berkeley",
select = c("Year", "Anomaly10y", "Unc10y"))
> clim <- clim[1:205,]
> str(clim)
'data.frame': 205 obs. of 3 variables:
$ Year : num 1800 1801 1802 1803 1804 ...
$ Anomaly10y: num -0.435 -0.453 -0.46 -0.493 -0.536 -0.541 -0.59 -0.695 -0.763 -0.818 ...
$ Unc10y : num 0.505 0.493 0.486 0.489 0.483 0.475 0.468 0.461 0.453 0.451 ...
> ggplot(clim,aes(Year, Anomaly10y)) +
geom_ribbon(aes(ymin=Anomaly10y-Unc10y, ymax=Anomaly10y+Unc10y), alpha = .2) +
geom_line()
# ymin 和 ymax 不可去掉
# 上图过程等同于
ggplot(clim,aes(Year, Anomaly10y)) +
geom_line() + #main
geom_line(aes(x = Year, y = Anomaly10y-Unc10y),
colour = "grey50", linetype = "dashed") + #lower
geom_line(aes(x = Year, y = Anomaly10y+Unc10y),
colour = "grey50", linetype = "dashed") #upper