參考https://ggplot2.tidyverse.org/reference/
1.geom_abline和geom_hline
ggplot(mtcars) +
geom_point(aes(mpg, disp, colour = gear)) +
theme_bw()+
geom_hline(yintercept = c(300, 400), colour = 'red', linetype = 2, size = 2) +
geom_vline(xintercept = c(20, 25), colour = 'blue', linetype = 3, size = 3)

image.png
2.geom_bar和geom_col
2.1 count or weight 數(shù)量或權(quán)重
g <- ggplot(mpg, aes(class))
g + geom_bar() #count
g + geom_bar(aes(weight = displ)) #weight

count

weight
2.2 方向,把數(shù)據(jù)賦值給y,則轉(zhuǎn)為橫向
ggplot(mpg) + geom_bar(aes(y = class))

橫向
2.3 position
p <- ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity))
#default position stack
p + geom_bar()
#identity由于遮擋問(wèn)題不適合做bar圖
p + geom_bar(position = "identity")
p + geom_bar(position = "identity", fill = NA, aes(colour = clarity))
p + geom_bar(position = "fill")
p + geom_bar(position = "dodge")

stack

identity

identity透明

fill

dodge