R语言实战(第3版)第四章代码

#------------------------------------------------------------#
# R in Action (3rd ed): Chapter 4                            #
# Getting started with graphs                                #
# requires the ggplot2 and mosaicData packages               #
# install.packages(c("ggplot2", "mosaicData"))               #
#------------------------------------------------------------#

# -- Section 4.1 Creating a graph with ggplot2
library(ggplot2)
library(mosaicData)
data(CPS85)

# Figure 4.1 Mapping worker experience and wages to the x and y axes
ggplot(data = CPS85, mapping = aes(x = exper, y = wage))

# Figure 4.2 Scatterplot of worker experience vs wages
ggplot(data = CPS85, mapping = aes(x = exper, y = wage)) +
  geom_point()

# Figure 4.3 Scatterplot of worker experience vs. wages with outlier removed
CPS85 <- CPS85[CPS85$wage < 40, ]
ggplot(data = CPS85, mapping = aes(x = exper, y = wage)) +
  geom_point()

# Figure 4.4 Scatterplot of worker experience vs. wages with outlier removed
# with modified point color, transparency, and point size
ggplot(data = CPS85, mapping = aes(x = exper, y = wage)) +
  geom_point(color = "cornflowerblue", alpha = .7, size = 1.5) +
  theme_bw()

# Figure 4.5 Scatterplot of worker experience vs. wages
# with a line of best fit
ggplot(data = CPS85, 
       mapping = aes(x = exper, y = wage)) +
  geom_point(color = "cornflowerblue", alpha = .7, size = 1.5) +
  theme_bw() +
  geom_smooth(method = "lm")

# Figure 4.6 Scatterplot of worker experience vs. wages
# with points colored by sex and separate line of best
# fit for men and women.
ggplot(data = CPS85, 
       mapping = aes(x = exper, y = wage,
                     color = sex, shape = sex, linetype = sex)) +
  geom_point(alpha = .7, size = 1.5) +
  geom_smooth(method = "lm", se = FALSE, size = 1.5) +
  theme_bw()

# Figure 4.7 Scatterplot of worker experience vs. wages
# with custom x- and y-axes and custom color mappings for sex.
ggplot(data = CPS85,
  mapping = aes(x = exper, y = wage,
                color = sex, shape = sex, linetype = sex)) +
  geom_point(alpha = .7, size = 1.5) +
  geom_smooth(method = "lm", se = FALSE, size = 1.5) +
  scale_x_continuous(breaks = seq(0, 60, 10)) +
  scale_y_continuous(breaks = seq(0, 30, 5)) +
  scale_color_manual(values = c("indianred3", "cornflowerblue")) +
  theme_bw()

# Figure 4.8 Scatterplot of worker experience vs. wages with
# custom x- and y-axes and custom color mappings for sex.
# Wages are printed in dollar format.
ggplot(
  data = CPS85,
  mapping = aes(x = exper, y = wage,
                color = sex, shape = sex, linetype = sex)) +
  geom_point(alpha = .7, size = 1.5) +
  geom_smooth(method = "lm", se = FALSE, size = 1.5) +
  scale_x_continuous(breaks = seq(0, 60, 10)) +
  scale_y_continuous(breaks = seq(0, 30, 5),
                     label = scales::dollar) +
  scale_color_manual(values = c("indianred3", "cornflowerblue")) +
  theme_bw()

# Figure 4.9 Scatterplot of worker experience vs. wages with
# custom x- and y-axes and custom color mappings for sex.
# Separate graphs (facets) are provided for each of 8 job sectors.
ggplot(
  data = CPS85,
  mapping = aes(x = exper, y = wage,
                color = sex, shape = sex, linetype = sex)) +
  geom_point(alpha = .7) +
  geom_smooth(method = "lm", se = FALSE) +
  scale_x_continuous(breaks = seq(0, 60, 10)) +
  scale_y_continuous(breaks = seq(0, 30, 5),
                     label = scales::dollar) +
  scale_color_manual(values = c("indianred3", "cornflowerblue")) +
  facet_wrap(~sector) +
  theme_bw()

# Figure 4.10 Scatterplot of worker experience vs. wages
# with separate graphs (facets) for each of 8 job sectors
# and custom titles and labels.
ggplot(
  data = CPS85,
  mapping = aes(x = exper, y = wage,
                color = sex, shape = sex, linetype = sex)) +
  geom_point(alpha = .7) +
  geom_smooth(method = "lm", se = FALSE) +
  scale_x_continuous(breaks = seq(0, 60, 10)) +
  scale_y_continuous(breaks = seq(0, 30, 5),
                     label = scales::dollar) +
  scale_color_manual(values = c("indianred3","cornflowerblue")) +
  facet_wrap(~sector) +
  labs(title = "Relationship between wages and experience",
       subtitle = "Current Population Survey",
       caption = "source: http://mosaic-web.org/",
       x = " Years of Experience",
       y = "Hourly Wage",
       color = "Gender", shape = "Gender", linetype = "Gender") +
  theme_bw()

# Figure 4.11 Scatterplot of worker experience vs. wages
# with separate graphs (facets) for each of 8 job sectors
# and custom titles and labels, and a cleaner theme.

ggplot(data = CPS85,
       mapping = aes(x = exper, y = wage, color = sex, shape=sex,
                     linetype = sex)) +
  geom_point(alpha = .7) +
  geom_smooth(method = "lm", se = FALSE) +
  scale_x_continuous(breaks = seq(0, 60, 10)) +
  scale_y_continuous(breaks = seq(0, 30, 5),
                     label = scales::dollar) +
  scale_color_manual(values = c("indianred3", "cornflowerblue")) +
  facet_wrap(~sector) +
  labs(title = "Relationship between wages and experience",
       subtitle = "Current Population Survey",
       caption = "source: http://mosaic-web.org/",
       x = " Years of Experience",
       y = "Hourly Wage",
       color = "Gender", shape="Gender", linetype="Gender") +
  theme_minimal()

# -- Section 4.2 ggplot2 details

# Figure 4.12. Scatterplot of experience and wage by sex,
# where aes(color=sex) is placed in the ggplot() function.
# The mapping is applied to both the geom_point() and geom_smooth(),
# producing separate point colors for males and females,
# along with separate lines of best fit
ggplot(CPS85,
       mapping = aes(x = exper, y = wage, color = sex)) +
  geom_point(alpha = .7, size = 1.5) +
  geom_smooth(method = "lm", se = FALSE, size = 1)

# Figure 4.13 Scatterplot of experience and wage by sex,
# where aes(color=sex) is placed in the geom_point() function.
# The mapping is applied to point color producing separate point
# colors for men and women, but a single line of best fit
# for or all workers.
ggplot(CPS85, aes(x = exper, y = wage)) +
  geom_point(aes(color = sex), alpha = .7, size = 1.5) +
  geom_smooth(method = "lm", se = FALSE, size = 1)

# Listing 4.1 Using a ggplot2 graph as an object
data(CPS85, package = "mosaicData")
CPS85 <- CPS85[CPS85$wage < 40, ]



myplot <- ggplot(data = CPS85, aes(x = exper, y = wage)) +
  geom_point()

myplot

myplot2 <- myplot + geom_point(size = 3, color = "blue")
myplot2

myplot + geom_smooth(method = "lm") +
  labs(title = "Mildly interesting graph")

# section 4.2.4
# Listing 4.15
ggplot(CPS85, aes(x = exper, y = wage, color = "blue")) +
  geom_point() 

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值