?ggplot2
install.packages(“gapminder”)
## Classes 'tbl_df', 'tbl' and 'data.frame': 1704 obs. of 6 variables:
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num 28.8 30.3 32 34 36.1 ...
## $ pop : int 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num 779 821 853 836 740 ...
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data = gap, aes(x = lifeExp)) +
##使用十個藍色長條
geom_histogram(fill = "blue", color = "black", bins = 10) +
##設定標題
ggtitle("Life expectancy for the gap dataset") +
##設定兩軸
xlab("Life expectancy (years)") +
ylab("Frequency") +
##設定主題
theme_classic() ## 定義x、y,依continent用不同的顏色表示
ggplot(data = gap, aes(x = continent, y = lifeExp, fill = continent)) +
geom_boxplot() +
ggtitle("Boxplots for lifeExp by continent") +
xlab("Continent") +
ylab("Life expectancy (years)") +
##設定標題,以及x、y軸名稱
theme_minimal()What happens if you un-hashtage guides(fill = FALSE) and the plus sign in lines 68 and 69 above?
取消圖例
## 定義x、y,依continent用不同的顏色、形狀表示
ggplot(data = gap, aes(x = lifeExp, y = gdpPercap, color = continent, shape = continent)) +
geom_point(size = 5, alpha = 0.5) +
##點點的大小、透明度
theme_classic() +
##設定主題
ggtitle("Scatterplot of life expectancy by gdpPercap") +
xlab("Life expectancy (years)") +
ylab("gdpPercap (USD)") +
##設定標題,以及x、y軸名稱
theme(legend.position = "top",
##把圖例移到最上方
plot.title = element_text(hjust = 0.5, size = 20),
##設定標題大小和位置
legend.title = element_text(size = 10),
##設定圖例標題大小
legend.text = element_text(size = 10),
##設定圖例大小
axis.text.x = element_text(angle = 45, hjust = 1)) In lines the ggplot code above, what are the arguments inside of our second “theme” argument doing?
調整圖像的元素:標題、圖例、x軸