ggplot2

wxy 2019/11/13

library(tidyverse)
## -- Attaching packages --------------------------------------------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --

## √ ggplot2 3.2.1     √ purrr   0.3.2
## √ tibble  2.1.2     √ dplyr   0.8.3
## √ tidyr   1.0.0     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.4.0

## Warning: package 'ggplot2' was built under R version 3.6.1

## Warning: package 'tidyr' was built under R version 3.6.1

## Warning: package 'dplyr' was built under R version 3.6.1

## -- Conflicts ------------------------------------------------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)
child2016<- read_csv("../child2016change.csv")
## Parsed with column specification:
## cols(
##   age = col_double(),
##   gender = col_double(),
##   urban = col_double(),
##   edu_expect = col_double(),
##   parents_edu = col_double(),
##   finc = col_double(),
##   care_edu = col_double(),
##   communicate = col_double(),
##   total_care = col_double(),
##   tutor_fees_log = col_double(),
##   school_fees_log = col_double(),
##   other_log = col_double()
## )

关于图例

basic <- ggplot(child2016,aes(x = school_fees_log,y = other_log ,alpha = age)) +
  geom_point(color = "red") +
  theme_set(theme_bw()) +
  labs(title = "HAPPY ")
basic

为什么这个变不了

basic + scale_colour_hue()

basic + scale_colour_hue("how are you",
                         labels = c("hello","hi","bye","11","22"))

### 这个也不知道为什么没有用

basic + xlab("xixi")

basic + ylab("heihei")

basic +labs(x = "曹圭贤",y = "李赫宰",colour = "我想应该可以用中文")

mpg %>% head()
## # A tibble: 6 x 11
##   manufacturer model displ  year   cyl trans  drv     cty   hwy fl    class
##   <chr>        <chr> <dbl> <int> <int> <chr>  <chr> <int> <int> <chr> <chr>
## 1 audi         a4      1.8  1999     4 auto(~ f        18    29 p     comp~
## 2 audi         a4      1.8  1999     4 manua~ f        21    29 p     comp~
## 3 audi         a4      2    2008     4 manua~ f        20    31 p     comp~
## 4 audi         a4      2    2008     4 auto(~ f        21    30 p     comp~
## 5 audi         a4      2.8  1999     6 auto(~ f        16    26 p     comp~
## 6 audi         a4      2.8  1999     6 manua~ f        18    26 p     comp~
p <- qplot(cty,hwy,data = mpg,colour = drv)
p + scale_colour_hue()

p + scale_colour_hue("how are you",
                     labels = c("hello","hi","bye"))

mpg %>% head()
## # A tibble: 6 x 11
##   manufacturer model displ  year   cyl trans  drv     cty   hwy fl    class
##   <chr>        <chr> <dbl> <int> <int> <chr>  <chr> <int> <int> <chr> <chr>
## 1 audi         a4      1.8  1999     4 auto(~ f        18    29 p     comp~
## 2 audi         a4      1.8  1999     4 manua~ f        21    29 p     comp~
## 3 audi         a4      2    2008     4 manua~ f        20    31 p     comp~
## 4 audi         a4      2    2008     4 auto(~ f        21    30 p     comp~
## 5 audi         a4      2.8  1999     6 auto(~ f        16    26 p     comp~
## 6 audi         a4      2.8  1999     6 manua~ f        18    26 p     comp~
p <- qplot(cty,hwy,data = mpg,colour = drv)
p + xlab("xixi")

p + ylab("heihei")

p +labs(x = "曹圭贤",y = "李赫宰",colour = "我想应该可以用中文")#colour居然是图例的标签,

basic

basic + scale_x_continuous(breaks = c(5,10.0))#只是横坐标只显示5h和10.0

basic + scale_x_continuous(limits = c(5,7.5))#这是把数据限制到5到7.5之间的了
## Warning: Removed 961 rows containing missing values (geom_point).

ggplot(child2016,aes(x = school_fees_log,y = other_log,color = "red")) +
   geom_point()

# 下面两种做法生成同样的结果,只是坐标刻度标签不同,建议用第一种
ggplot(child2016,aes(x = log10(school_fees_log),y = log10(other_log),color = "red")) +#对标度进行坐标变换(坐标轴的也一起变了)
   geom_point()

ggplot(child2016,aes(x = school_fees_log,y = other_log,color = "red")) +
   geom_point() +
   scale_x_log10() +#对数据进行变换(只变了数据,还是用的以前的坐标刻度,拉过去了)
   scale_y_log10()
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Transformation introduced infinite values in continuous y-axis

f2d <- with(faithful,MASS::kde2d(eruptions,waiting,h = c(1,10),n = 50))
df <- with(f2d,cbind(expand.grid(x,y),as.vector(z)))
names(df) <- c("eruptions","waiting","density")
erupt <- ggplot(df,aes(waiting,eruptions,fill = density)) +
  geom_tile() +
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0))
erupt + scale_fill_gradient(limits = c(0,0.04))

erupt + scale_fill_gradient(limits = c(-0.04,0.04),low = "blue",high = "red")#双色梯度,用low和high控制梯度两端的颜色

erupt + scale_fill_gradient2(limits = c(0,0.04),low = "blue",high = "red")#三色梯度,用low和high控制梯度两端的颜色,不过中间还有一个中间色,默认值为0

erupt + scale_fill_gradient2(limits = c(-0.04,0.04),low = "blue",high = "red")

erupt + scale_fill_gradient2(limits = c(-0.04,0.04),midpoint = mean(df$density))#三色梯度,用midpoint将其设置为任意值

ggplot(child2016,aes(x = school_fees_log,y = other_log,color = "red")) +
   geom_point()+
  scale_fill_gradient(low = "blue",high = "red")

# library(vcd)
# fill_gradn <- function(pal){
#   scale_fill_gradientn(colours = pal(7),limits = c(0,0.04))
# }
# erupt + fill_gradn(rainbow_hcl)
# erupt + fill_gradn(diverge_hcl)
# erupt + fill_gradn(heat_hcl)
point <- qplot(brainwt,bodywt,data = msleep,log = "xy",colour = vore)
area <- qplot(log10(brainwt),data = msleep,fill = vore,binwidth = 1)

point + scale_colour_brewer(palette = "Set1")
## Warning: Removed 32 rows containing missing values (geom_point).

point + scale_colour_brewer(palette = "Set2")
## Warning: Removed 32 rows containing missing values (geom_point).

point + scale_colour_brewer(palette = "PasteL1")
## Warning in pal_name(palette, type): Unknown palette PasteL1

## Warning in pal_name(palette, type): Removed 32 rows containing missing
## values (geom_point).

area + scale_fill_brewer(palette = "Set1")#别忘了改成fill
## Warning: Removed 27 rows containing non-finite values (stat_bin).

area + scale_fill_brewer(palette = "Set2")
## Warning: Removed 27 rows containing non-finite values (stat_bin).

area + scale_fill_brewer(palette = "PasteL1")
## Warning in pal_name(palette, type): Unknown palette PasteL1

## Warning: Removed 27 rows containing non-finite values (stat_bin).

scale_colour_manual(values = c("red","orange","yellow","green","blue"))
## <ggproto object: Class ScaleDiscrete, Scale, gg>
##     aesthetics: colour
##     axis_order: function
##     break_info: function
##     break_positions: function
##     breaks: waiver
##     call: call
##     clone: function
##     dimension: function
##     drop: TRUE
##     expand: waiver
##     get_breaks: function
##     get_breaks_minor: function
##     get_labels: function
##     get_limits: function
##     guide: legend
##     is_discrete: function
##     is_empty: function
##     labels: waiver
##     limits: NULL
##     make_sec_title: function
##     make_title: function
##     map: function
##     map_df: function
##     n.breaks.cache: NULL
##     na.translate: TRUE
##     na.value: NA
##     name: waiver
##     palette: function
##     palette.cache: NULL
##     position: left
##     range: <ggproto object: Class RangeDiscrete, Range, gg>
##         range: NULL
##         reset: function
##         train: function
##         super:  <ggproto object: Class RangeDiscrete, Range, gg>
##     reset: function
##     scale_name: manual
##     train: function
##     train_df: function
##     transform: function
##     transform_df: function
##     super:  <ggproto object: Class ScaleDiscrete, Scale, gg>
colours <- c(carni = "red","NA" = "orange",insecti = "yellow",herbi = "greeen",omni = "blue") 
#plot + aes(colour = vore) + scale_colour_manual(values = colours)
#plot + aes(shape = vore) +
 # scael_shape_manual(values = c(1,2,6,0,23))