Reputation: 75
I am having troubles with the following piece of code
ggplot(entidad, aes(x=Año, y=promedio, group=Entidad, color=Entidad)) +
geom_point(aes(shape = Entidad), size = 3, position = position_dodge(0.2)) +
geom_errorbar(aes(ymin=promedio-desviación, ymax=promedio+desviación, color = Entidad), width=0.4,
position=position_dodge(0.2)) +
ylim(150,350) + xlim(2015.7,2019.3) +
ylab("Promedio (desviación estándar)") +
geom_text(data = entidad[entidad$Entidad=="ZIPAQUIRÁ",],aes(label = promedio, y = promedio), hjust = 1.8) +
geom_text(data= entidad[entidad$Entidad=="Nacional",], aes(label = promedio, y = promedio), hjust = -0.9) +
geom_text(data = entidad[entidad$Entidad=="ZIPAQUIRÁ",], aes(label = paste("(",desviación,")"), y = promedio),
hjust = 1.5, vjust = 2) +
geom_text(data = entidad[entidad$Entidad=="Nacional",], aes(label = paste("(",desviación,")"), y = promedio),
hjust = -0.45, vjust = 2) +
scale_color_manual(values=c(rgb(0,81,160, maxColorValue = 255),
rgb(58, 170, 53, maxColorValue = 255)),
breaks = c("ZIPAQUIRÁ", "Nacional")) +
#scale_fill_manual(values = c(rgb(116, 141, 199, maxColorValue = 255),
# Nacional = rgb(164, 200, 70, maxColorValue = 255)),
# breaks = c("ZIPAQUIRÁ", "Nacional")) +
theme(
panel.background = element_rect(fill = "white", colour = rgb(198,
198,
198,
maxColorValue = 255),
size = 1, linetype = "solid"),
#panel.grid.major = element_line(size = 0.1, linetype = 'dashed',
# colour = rgb(198,198,198,
# maxColorValue = 255))
panel.grid.minor = element_line(size = 0.1, linetype = 'dashed',
colour = rgb(198,198,198,
maxColorValue = 255))
)
Which returns the plot:
I do not understand why is that I have two legends labelled "Entidad", I only want the bottom one to appear. My second concern is that I would like the item "ZIPAQUIRÁ" to be color blue, and "Nacional" to be color green.
Does anyone know how I can achieve this?
Upvotes: 0
Views: 107
Reputation: 3660
You have two legends for "Entidad" because you have it mapped to two aestetics - shape
and color
.
To remove the legend for an aesthetic (in your case the upper legend is for shape
) you can just add (with a +
)
guides(shape = FALSE)
Colors in scale_color_manual
follow the order of factors, so if you want "ZIPAQUIRÁ" (the second factor, they're alphabetic regardless of the order you specify for breaks
) to be blue, make blue your second color like this:
scale_color_manual(values=c(rgb(58, 170, 53, maxColorValue = 255),
rgb(0,81,160, maxColorValue = 255)),
breaks = c("ZIPAQUIRÁ", "Nacional"))
Some simulated data to make this reproducible:
set.seed(123)
entidad <- data.frame(Año = c(2016, 2016, 2017, 2017, 2018, 2018, 2019, 2019),
promedio = as.integer(runif(8, 200, 300)),
desviación = as.integer(runif(8, 25, 50)),
Entidad = rep(c("ZIPAQUIRÁ", "Nacional"), 2))
ggplot(entidad, aes(
x = Año,
y = promedio,
group = Entidad,
color = Entidad
)) +
geom_point(aes(shape = Entidad),
size = 3,
position = position_dodge(0.2)) +
geom_errorbar(
aes(
ymin = promedio - desviación,
ymax = promedio + desviación,
color = Entidad
),
width = 0.4,
position = position_dodge(0.2)
) +
ylim(150, 350) + xlim(2015.7, 2019.3) +
geom_text(data = entidad[entidad$Entidad == "ZIPAQUIRÁ", ],
aes(label = promedio, y = promedio),
hjust = 1.8) +
geom_text(data = entidad[entidad$Entidad == "Nacional", ],
aes(label = promedio, y = promedio),
hjust = -0.9) +
geom_text(
data = entidad[entidad$Entidad == "ZIPAQUIRÁ", ],
aes(label = paste("(", desviación, ")"), y = promedio),
hjust = 1.5,
vjust = 2
) +
geom_text(
data = entidad[entidad$Entidad == "Nacional", ],
aes(label = paste("(", desviación, ")"), y = promedio),
hjust = -0.45,
vjust = 2
) +
scale_color_manual(values = c(
rgb(58, 170, 53, maxColorValue = 255),
rgb(0, 81, 160, maxColorValue = 255)
),
breaks = c("ZIPAQUIRÁ", "Nacional")) +
theme(
panel.background = element_rect(
fill = "white",
colour = rgb(198,
198,
198,
maxColorValue = 255),
size = 1,
linetype = "solid"
),
panel.grid.minor = element_line(
size = 0.1,
linetype = 'dashed',
colour = rgb(198, 198, 198,
maxColorValue = 255)
)
) +
guides(shape = FALSE)
Upvotes: 1