library(ggplot2)
library(ggrepel)Warning: package 'ggrepel' was built under R version 4.5.2
data<- read.csv("C:/02.Rstatistics/0320/midwest.csv")
View(data)載入套件和資料
library(ggplot2)
library(ggrepel)Warning: package 'ggrepel' was built under R version 4.5.2
data<- read.csv("C:/02.Rstatistics/0320/midwest.csv")
View(data)畫點狀圖
figure <- ggplot(data, mapping=aes(x=area,y=poptotal))
figure1 <- figure+geom_point();figure1figure2 <- figure1+xlim(0,0.1)+ylim(0,1000000);figure2Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
也可以一行寫完 figure2.1 <-ggplot(data, mapping=aes(x=area,y=poptotal))+ geom_point()+xlim(0,0.1)+ylim((0,1000000));figure2.1
figure3 <- figure2+ggtitle("Area vs.Total Population",
subtitle="From Midwest Data")+
xlab("Area")+ylab("Population");figure3Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
figure4 <- ggplot(data, mapping = aes(x=area,y=poptotal))+
geom_point(aes(col=state),size=2)+
xlim(0,0.1)+ylim(0,1000000)+
ggtitle("Area vs. Total Data")+
xlab("Area")+ylab("Population");figure4Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
#更換色系
figure5 <- figure4+ scale_color_brewer(palette = "PRGn"); figure5Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
#將標題至中
figure6 <- figure5+theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust=0.5));figure6Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
附加功能
#白底
figure6 + theme_bw()Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
#去除框限
figure6 + theme_classic()Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
#去除網格
figure6 + theme_void()Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
figure7 <- ggplot(data, mapping = aes(x=area,y=poptotal))+
geom_point(aes(col=state,size=popdensity))+
xlim(0,0.1)+ylim(0,1000000)+
ggtitle("Area vs. Total Data")+
xlab("Area")+ylab("Population");figure4Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
figure8 <- figure7+ labs(color="STATE",size="Density");figure8Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
figure9 <- figure8+ scale_color_manual(name="STATE",
labels=c("Illinois",
"Indiana",
"Michigan",
"Ohio",
"Wisconsin"),
values=c("IL"="lightgoldenrod2",
"IN"="salmon2",
"MI"="lightskyblue2",
"OH"="orchid2",
"WI"="darkseagreen2"))