我有以下安全代码:
ggplot(data = afirmaciones[afirmaciones$prueba==x,
],
aes(x = 1,
y = Afirmacion_d)) +
geom_point(size = 20,
stroke = 5,
aes(color = porcentaje),
show.legend = F) + facet_wrap(~agregacion) +
geom_text(aes(label = porcentaje),
color = "white",
size = 8) +
theme(
panel.background = element_rect(color = "#575756",
size = 1.1,
linetype = "solid",
fill = "white"),
panel.grid.major.y = element_line(size = 0.1,
linetype = "dotted",
color = "#575756"),
axis.title = element_blank(),
axis.text.x = element_blank(),
strip.background = element_rect(fill = "white"),
strip.text = element_text(colour = "#575756",
size = 20),
axis.text.y = element_text(color = "#575756",
size = 22)
)
哪个返回以下图:
我想根据变量“ porcentaje”的数量为每个气泡着色。但是我有一些特定的颜色,如果变量“ porcentaje”落入特定的间隔,我希望将气泡着色。我想到的第一件事是生成一个变量“ colores”,如果var跟随该变量,则该变量具有颜色,然后在末尾使用scale_color_manual(values = afirmaciones[afirmaciones$prueba==x, "colores"])
将其添加,但是它不起作用。每种颜色的条件如下:
afirmaciones$colores <- NA
afirmaciones[afirmaciones$agregacion=="Nacional" |
afirmaciones$porcentaje < 20,"colores"] <- "#74ad68"
afirmaciones[afirmaciones$agregacion=="Nacional" |
afirmaciones$porcentaje > 20 &
afirmaciones$porcentaje < 40,"colores"] <- "#e0e165"
afirmaciones[afirmaciones$agregacion=="Nacional" |
afirmaciones$porcentaje > 40 &
afirmaciones$porcentaje < 70,"colores"] <- "#de9452"
afirmaciones[afirmaciones$agregacion=="Nacional" |
afirmaciones$porcentaje > 70,"colores"] <- "#cb4c43"
afirmaciones[afirmaciones$agregacion=="SENA" |
afirmaciones$porcentaje < 20,"colores"] <- "#74ad68"
afirmaciones[afirmaciones$agregacion=="SENA" |
afirmaciones$porcentaje > 20 &
afirmaciones$porcentaje < 40,"colores"] <- "#e0e165"
afirmaciones[afirmaciones$agregacion=="SENA" |
afirmaciones$porcentaje > 40 &
afirmaciones$porcentaje < 70,"colores"] <- "#de9452"
afirmaciones[afirmaciones$agregacion=="SENA" |
afirmaciones$porcentaje > 70,"colores"] <- "#cb4c43"
afirmaciones[afirmaciones$agregacion=="Diferencia" &
afirmaciones$porcentaje<=-5,"colores"] <- "#74ad68"
afirmaciones[afirmaciones$agregacion=="Diferencia" &
afirmaciones$porcentaje>-5 &
afirmaciones$porcentaje<=-0.1,"colores"] <- "#e0e165"
afirmaciones[afirmaciones$agregacion=="Diferencia" &
afirmaciones$porcentaje>-0.1 &
afirmaciones$porcentaje<=4.9,"colores"] <- "#de9452"
afirmaciones[afirmaciones$agregacion=="Diferencia" &
afirmaciones$porcentaje>5,"colores"] <- "#cb4c43"
这样做的结果是:
但是在该图中分配的颜色是错误的。有谁知道我该怎么解决?
我的数据可以通过以下方式复制:
structure(list(agregacion = structure(c(2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("SENA", "Nacional", "Diferencia"), class = "factor"),
Afirmacion_d = structure(c(12L, 12L, 11L, 11L, 10L, 10L,
9L, 9L, 8L, 8L, 7L, 7L, 6L, 6L, 12L, 12L, 3L, 3L, 2L, 2L,
1L, 1L, 11L, 11L, 10L, 10L, 9L, 9L, 8L, 8L, 7L, 7L, 6L, 6L,
5L, 5L, 4L, 4L, 12L, 12L, 11L, 11L, 10L, 10L, 12L, 12L, 11L,
11L, 12L, 12L, 11L, 11L, 12L, 12L, 11L, 11L, 10L, 10L, 12L,
11L, 10L, 9L, 8L, 7L, 6L, 12L, 3L, 2L, 1L, 11L, 10L, 9L,
8L, 7L, 6L, 5L, 4L, 12L, 11L, 10L, 12L, 11L, 12L, 11L, 12L,
11L, 10L), .Label = c("Afirmación 12", "Afirmación 11", "Afirmación 10",
"Afirmación 9", "Afirmación 8", "Afirmación 7", "Afirmación 6",
"Afirmación 5", "Afirmación 4", "Afirmación 3", "Afirmación 2",
"Afirmación 1"), class = "factor"), prueba = c("competencias",
"competencias", "competencias", "competencias", "competencias",
"competencias", "competencias", "competencias", "competencias",
"competencias", "competencias", "competencias", "competencias",
"competencias", "ensamblaje", "ensamblaje", "ensamblaje",
"ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje",
"ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje",
"ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje",
"ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje",
"ensamblaje", "lectura", "lectura", "lectura", "lectura",
"lectura", "lectura", "mantenimiento", "mantenimiento", "mantenimiento",
"mantenimiento", "promoción", "promoción", "promoción", "promoción",
"razonamiento", "razonamiento", "razonamiento", "razonamiento",
"razonamiento", "razonamiento", "competencias", "competencias",
"competencias", "competencias", "competencias", "competencias",
"competencias", "ensamblaje", "ensamblaje", "ensamblaje",
"ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje",
"ensamblaje", "ensamblaje", "ensamblaje", "ensamblaje", "lectura",
"lectura", "lectura", "mantenimiento", "mantenimiento", "promoción",
"promoción", "razonamiento", "razonamiento", "razonamiento"
), porcentaje = c("51", "53", "51", "52", "61", "62", "57",
"58", "47", "49", "55", "56", "48", "49", "61", "61", "49",
"50", "47", "48", "60", "60", "64", "65", "50", "50", "55",
"55", "61", "62", "41", "42", "38", "39", "36", "36", "50",
"50", "53", "54", "43", "43", "53", "54", "54", "55", "49",
"50", "50", "51", "45", "44", "48", "49", "63", "64", "57",
"57", "2", "1", "1", "1", "2", "1", "1", "0", "1", "1", "0",
"1", "0", "0", "1", "1", "1", "0", "0", "1", "0", "1", "1",
"1", "1", "-1", "1", "1", "0"), colores = c("#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#cb4c43", "#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#cb4c43", "#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#cb4c43", "#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#cb4c43", "#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#cb4c43", "#de9452", "#cb4c43",
"#e0e165", "#cb4c43", "#e0e165", "#cb4c43", "#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#cb4c43", "#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#cb4c43", "#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#cb4c43", "#de9452", "#cb4c43",
"#de9452", "#cb4c43", "#de9452", "#de9452", "#de9452", "#de9452",
"#de9452", "#de9452", "#de9452", "#de9452", "#de9452", "#de9452",
"#de9452", "#de9452", "#de9452", "#de9452", "#de9452", "#de9452",
"#de9452", "#de9452", "#de9452", "#de9452", "#de9452", "#de9452",
"#de9452", "#de9452", "#de9452", "#e0e165", "#de9452", "#de9452",
"#de9452")), row.names = c("56772", "76081", "56752", "76086",
"56771", "76085", "56781", "76080", "56775", "76083", "56753",
"76084", "59239", "76082", "59235", "76108", "59236", "76107",
"56776", "76103", "56754", "76099", "56749", "76104", "59238",
"76100", "56764", "76106", "59240", "76102", "56763", "76098",
"56755", "76105", "56760", "76101", "59237", "76097", "56777",
"76088", "56773", "76089", "56761", "76087", "56750", "76095",
"56765", "76096", "56779", "76093", "56778", "76094", "56757",
"76090", "56756", "76091", "56766", "76092", "760811", "760861",
"760851", "760801", "760831", "760841", "760821", "761081", "761071",
"761031", "760991", "761041", "761001", "761061", "761021", "760981",
"761051", "761011", "760971", "760881", "760891", "760871", "760951",
"760961", "760931", "760941", "760901", "760911", "760921"), class = "data.frame")
亲切的问候。
答案 0 :(得分:1)
有点黑,但是我想这就是您想要的(使用prueba == "ensamblaje"
:
pal <- c("#74ad68", "#e0e165", "#de9452", "#cb4c43")
afirmaciones$grp <- dplyr::case_when(
afirmaciones$porcentaje < 20 ~ "#74ad68",
afirmaciones$porcentaje > 20 & afirmaciones$porcentaje < 40 ~ "#e0e165",
afirmaciones$porcentaje > 40 & afirmaciones$porcentaje < 70 ~ "#de9452",
TRUE ~ "#cb4c43"
)
ggplot(data = afirmaciones[afirmaciones$prueba=="ensamblaje",
],
aes(x = 1,
y = Afirmacion_d)) +
geom_point(size = 20,
stroke = 5,
aes(color = grp),
show.legend = F) + facet_wrap(~agregacion) +
scale_color_manual(values = pal, limits = pal) +
geom_text(aes(label = porcentaje),
color = "white",
size = 8) +
theme(
panel.background = element_rect(color = "#575756",
size = 1.1,
linetype = "solid",
fill = "white"),
panel.grid.major.y = element_line(size = 0.1,
linetype = "dotted",
color = "#575756"),
axis.title = element_blank(),
axis.text.x = element_blank(),
strip.background = element_rect(fill = "white"),
strip.text = element_text(colour = "#575756",
size = 20),
axis.text.y = element_text(color = "#575756",
size = 22)
)
(通过更适当的缩放级别/更好的绘图大小,圆圈将看起来更像您想要的样子)