A snapshot of my correct plot, and question on its behavior.
The problem I am having is as follows: I have two datasets (trivially, in this case, I am using the pokemon dataset in highcharter as a source, and generating a second dataset from it).
I want to draw some averages in a box plot by type (but in a single colour), and then add a second series of points in the same categories on top, again in a single colour. Drawing the chart is easy but displaying the legend has proven to be anything but.
Below is my final solution for this issue, which gives me the plot behaviour I was after. What I would like to know is why I cannot use scale_color_manual to do the same to my points (ie change the colour of my points, and override the title of my legend) as I do to my boxes with scale_fill_manual? If I remove colour="orange"
from the line that creates the points, they revert to the default black even if I include the second piece of code which I believe should change them to yellow. Does scale_color_manual not refer to my points in this case, and if so, why?
library(highcharter)
library(dplyr)
myData <- pokemon
myData <- myData %>% mutate(type_1 = factor(type_1))
myList2 <- myData %>%
group_by(type_1) %>%
summarise(
meanHeight = mean(height),
meanWeight = mean(weight),
meanAttack = mean(attack)
)
ggplot(data = pokemon, aes(x=factor(type_1), y=defense)) +
geom_boxplot(aes(fill="")) +
geom_point(data=myList2, aes(x=type_1, y=meanAttack, shape=""), colour =
"orange") +
coord_flip() +
theme(panel.background = element_blank()) +
labs(x = "Pokemon Main Type", y = "Defense value", title = "My pokemon
plot", subtitle = "with a subtitle",
fill = "Type", shape = "Mean Attack",
caption = "data (c) the highcharter package") +
guides(fill=guide_legend(override.aes=list(shape=""))) +
scale_fill_manual('Pokemon types',
values = 'lightblue',
guide = guide_legend(override.aes = list(alpha = 1)))
The code which does nothing is:
+ scale_color_manual('mylegend', ## this does nothing
values = c('yellow'),
guide = guide_legend(title="this", override.aes =
list(alpha = 1)))
Thank you for any insights.
The primary problem here is the connection between aes()
and the scale_xx
commands. When you pass a column of your data frame to an aesthetic, for example aes(fill = type_1)
, ggplot examines all the values of that column to determine how fill should be represented. In this case, "type_1" is categorical, so ggplot would apply its default categorical hue scale, with its default coloring. To change those colors, you would use scale_fill_manual
and specify a list of colors.
Conversely, if you just want to set a segment of your data to one color, you would specify that outside of aes()
, as in, geom_point(color = 'orange')
. No need to include a scale function. But since you also want a label to appear in your legend, we need to trick ggplot a little:
ggplot(data= pokemon, aes(x = factor(type_1), y = defense)) +
geom_boxplot(fill = 'lightblue') +
geom_point(data = myList2, aes(x = type_1, y = meanAttack, color = 'Mean Attack')) +
coord_flip() +
scale_color_manual(values = 'orange') +
labs(color = NULL, x = 'Pokemon Main Type') +
theme_bw()
Here we pass ggplot an "aesthetic" with one value: "Mean Attack". Since color is now linked to an aesthetic, you can affect it in scale_color_manual
, and it shows up in the legend, labeled appropriately.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.