好困了,直接放补充练习答案,有人问再说呗
rm(list = ls())
library(ggplot2)
#theme_set(theme_bw(base_size = 14))#设置主题为白色(个人爱好)
#创建一个airquality的wind的直方图
qplot(x = Wind,data = airquality,binwidth = 2,color = I("red"))
#为每一个month创建一个wind的箱线图
airquality$Month <- factor(airquality$Month)#要弄成因子,变成分类变量
qplot(x = Month,y = Wind,data = airquality, geom = "boxplot")
#Wind和Ozone的关系,加标题,轴标签,让X轴从0开始,加一条直线
qplot(data = airquality,x = Wind,y = Ozone,
main = "Ozone & Wind",
xlab = "Wind (mph)",
ylab = "Ozone (ppb)",
xlim = c(0,25)) +
geom_smooth(method = lm,formula = y ~ x,se = F)
#使用demoData数据画Weight和Height 散点图用不同男女区分颜色,根据是否抽烟
#用不同符号
library(mangoTraining)
demoData <- mangoTraining::demoData
head(demoData)
qplot(data = demoData,x = Weight,y = Height ,color = Sex,shape = Smokes,size = 10)
#创建分面绘图,行是性别,列是吸烟与否
qplot(data = demoData,x = Weight,y = Height,facets =Sex ~ Smokes)
#绘制美国地图,每个州用不同的颜色表示,图例放在底部,将州化为10行
install.packages("maps")
install.packages("mapproj")
library(maps)
library(mapproj)
library(ggplot2)
state <- map_data("state")
head(state)
state$region <- factor(state$region)
par(mar=c(1,1,1,1))
p <- ggplot(state,aes(x = long,y = lat,group = region,col = region,fill = region))+
geom_polygon(colour="black") +
guides(fill = guide_legend(title = "state",
ncol = 10,
title.position = "top",
label.position = "top",
label.hjust = 0.5,
label.vjust = 1,
label.theme = ))+
theme(legend.position = "bottom")
dev.off()
#转换该图,使用墨卡托投影
p + coord_map("mercator")