我有一个简单的geom_bar和geom_point,想根据数据中的值对条和点进行着色。我的代码当前如下:
```df <- (Test3)
ggplot(Test3,aes(ques,diff1,subj))+
geom_bar(stat = "identity")+
geom_point(aes(x=ques, y= diff2))+
facet_grid(~subj)
我的图表当前如下所示:chart
我的数据如下:
```# A tibble: 8 x 9
subj ques `%` `sect%` diff1 ci last_y diff2 tt
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 geog q1 82.1 83.6 -1.49 10.1 78 4.14 2.1
2 geog q2 80 50 30 10.1 54 26 -1.28
3 hist q1 92.9 83.6 9.21 10.1 NA NA NA
4 hist q2 85.7 82.8 2.94 10.1 NA NA NA
5 Sports q1 73.8 82.4 -8.61 10.1 95 -21.2 2.97
6 Sports q2 69.2 81.8 -12.6 10.1 12 57.2 -2.59
7 sci q1 78.6 85.2 -6.66 10.1 87 -8.43 -3
8 sci q2 78 50 28 10.1 55 23 2
我想要的是,如果diff1> = ci,则条形为绿色,如果diff1 <= ci,则条形为红色,如果都不是,则条形为灰色。 如果tt> = 1.96,则这些点为绿色;如果tt <= 1.96,则这些点为红色;如果都不为灰色,则这些点为灰色。感谢您的协助。
答案 0 :(得分:0)
可以这样实现:
dplyr::mutate
和dplyr::case_when
color
或fill
上。scale_color/fill_identity
library(ggplot2)
library(dplyr)
Test3 <- Test3 %>%
mutate(
fill = case_when(
diff1 >= ci ~ "green",
diff1 < ci ~ "red",
TRUE ~ "grey"
),
color = case_when(
tt >= 1.96 ~ "green",
tt < 1.96 ~ "red",
TRUE ~ "grey"
)
)
ggplot(Test3, aes(ques, diff1, subj)) +
geom_col(aes(fill = fill)) +
geom_point(aes(x = ques, y = diff2, color = color)) +
scale_color_identity() +
scale_fill_identity() +
facet_grid(~subj)
#> Warning: Removed 2 rows containing missing values (geom_point).
数据
Test3 <- structure(list(subj = c(
"geog", "geog", "hist", "hist", "Sports",
"Sports", "sci", "sci"
), ques = c(
"q1", "q2", "q1", "q2", "q1",
"q2", "q1", "q2"
), `%` = c(
82.1, 80, 92.9, 85.7, 73.8, 69.2,
78.6, 78
), `sect%` = c(
83.6, 50, 83.6, 82.8, 82.4, 81.8, 85.2,
50
), diff1 = c(-1.49, 30, 9.21, 2.94, -8.61, -12.6, -6.66, 28), ci = c(10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1), last_y = c(
78L,
54L, NA, NA, 95L, 12L, 87L, 55L
), diff2 = c(
4.14, 26, NA, NA,
-21.2, 57.2, -8.43, 23
), tt = c(
2.1, -1.28, NA, NA, 2.97, -2.59,
-3, 2
)), class = "data.frame", row.names = c(
"1", "2", "3", "4",
"5", "6", "7", "8"
))