-
Notifications
You must be signed in to change notification settings - Fork 0
/
发散条图.qmd
87 lines (78 loc) · 2.75 KB
/
发散条图.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 发散条图
Diverging Bars
## geom_linerange
```{r}
library(data.table)
df <- datasets::mtcars %>% data.table() %>% .[,cars:=rownames(mtcars)]
df[,mpg_z:=scale(mpg)]
df[mpg_z>0,mpg_c:="g"]
df[mpg_z<0,mpg_c:="r"]
```
```{r}
ggplot(df,aes(y=reorder(cars,mpg_z),colour = mpg_c))+
geom_linerange(aes(xmin = 0,
xmax = mpg_z),
linewidth=2,alpha=.5,show.legend = F)+
scale_color_manual(values = c("r"="red","g"="green"))+
scale_x_continuous(limits = c(-2,2.5),breaks = seq(-2,2.5,0.5),expand = c(0,0.02))+
scale_y_discrete(expand = c(0.03,0.03))+
labs(x="Mileage",y="Model",title = "Diverging Bars of Car Mileage")+
theme_bw()+
theme(
aspect.ratio = 1/1.3,
axis.ticks = element_blank(),
axis.title = element_text(face = "italic"),
axis.text.y = element_text(size = 8),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = .5)
)+
geom_text(
data = df[mpg_c=="g"],
mapping = aes(x=mpg_z,label = sprintf("%.2f",mpg_z)),hjust = 0,colour = "green4",size = 2
)+
geom_text(
data = df[mpg_c=="r"],
mapping = aes(x=mpg_z,label = sprintf("%.2f",mpg_z)),hjust = 1,colour = "red4",size = 2
)
```
## geom_segment
```{r}
ggplot(df)+
geom_segment(aes(x = 0,xend = mpg_z,
y=reorder(cars,mpg_z),yend =reorder(cars,mpg_z),
colour=mpg_c),
linewidth=2,alpha=.5,show.legend = F)+
scale_color_manual(values = c("r"="red","g"="green"))+
scale_x_continuous(limits = c(-2,2.5),breaks = seq(-2,2.5,0.5),expand = c(0,0.02))+
scale_y_discrete(expand = c(0.03,0.03))+
labs(x="Mileage",y="Model",title = "Diverging Bars of Car Mileage")+
theme_bw()+
theme(
aspect.ratio = 1/1.3,
axis.ticks = element_blank(),
axis.title = element_text(face = "italic"),
axis.text.y = element_text(size = 8),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = .5)
)
```
## geom_bar
```{r}
ggplot(df,aes(x=reorder(cars,mpg_z),y=mpg_z,fill = mpg_c))+
geom_bar(stat = "identity",
width=0.5,alpha=.5,show.legend = F)+
scale_fill_manual(values = c("r"="red","g"="green"))+
scale_y_continuous(limits = c(-2,2.5),breaks = seq(-2,2.5,0.5),expand = c(0,0.02))+
scale_x_discrete(expand = c(0.03,0.03))+
labs(x="Mileage",y="Model",title = "Diverging Bars of Car Mileage")+
theme_bw()+
theme(
aspect.ratio = 1/1.3,
axis.ticks = element_blank(),
axis.title = element_text(face = "italic"),
axis.text.y = element_text(size = 8),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = .5)
)+
coord_flip()
```