I've made a barplot in ggplot, and added a couple of lines. What happens is that the color and description of the lines don't correspond:
The yellow line should have the description 'Median Member', but is displayed as 'avg Member'. What happens here? The code I used:
library(ggplot2)
library(dplyr)
MemberID=c(1,1,1, 2, 2, 2)
ClientCode = c(10,100,1000, 20, 200, 2000)
Duration = c(2356, 1560, 9000, 4569, 3123, 8000)
df <- data.frame(MemberID, ClientCode, Duration)
dr <- df %>%
filter(MemberID == 1)
dr_avg <- df
ggplot(dr, aes(reorder(as.character(ClientCode), -Duration), Duration, fill=-Duration)) +
geom_bar(stat="identity") + # the height of the bar will represent the value in a column of the data frame
xlab('ClientCode') +
ylab('Duration (Minutes)') +
geom_hline(data=dr, aes(yintercept=mean(Duration), linetype = 'Avg Member'), color = 'red', show.legend = TRUE) +
geom_hline(data=dr, aes(yintercept=median(Duration), linetype = 'Median Member'), color = 'orange', show.legend = TRUE) +
geom_hline(data=dr_avg, aes(yintercept=mean(Duration), linetype = 'Avg all data'), color = 'blue', show.legend = TRUE) +
scale_linetype_manual(name = "Line", values = c(2, 2, 2), guide = guide_legend(override.aes = list(color = c("red", "orange", "blue")))) +coord_flip()
Don't create geom_hline for every line you want to insert. What if you have hundred of them? Create a separate object d and specify different linetypes and colors there geom_hline(data = d, aes(yintercept = value, linetype = name, color = name)). When you want to specify colors use: scale_colour_manual(values = c("red", "orange", "blue")).
d1 <- summarize(df, mean(Duration), median(Duration))
d2 <- summarize(dr_avg, mean(Duration))
d <- data.frame(value = as.numeric(c(d1, d2)),
name = c('Avg Member', 'Median Member', 'Avg all data'))
ggplot(dr, aes(reorder(as.character(ClientCode), -Duration),
Duration,
fill = factor(-Duration))) +
geom_bar(stat = "identity") +
labs(x = "ClientCode",
y = "Duration (Minutes)") +
geom_hline(data = d, aes(yintercept = value, linetype = name, color = name)) +
scale_fill_brewer(palette = "Dark2") +
scale_colour_manual(values = c("red", "orange", "blue")) +
coord_flip() +
theme_bw()
PS.: Data that you provided doesn't make sense as two lines overlap.
Related
I've been trying for a while now and also doing a lot of research, but I just can't get it to add a simple legend for my two lines.
I have two lines in my chart and I just want to add a legend for the two lines.
scale_color_manual did not work. I suspect it is because I am using scale_y_break. In other plots (without scale_y_break) scale_color_manual works without problems.
Here is my code:
day <- c(1:5)
altimeter <- c(8.291, 8.872, 7.212, 8.1, 5.92)
slope_kilometers <- c(30.23, 34.8, 29.34, 32.98, 21.23)
df2 <- data.frame(day, altimeter, slope_kilometers)
library(ggbreak)
altimeter_color <- "steelblue"
slope_kilometers_color <- "darkred"
ggplot(df2, aes(x = day)) +
#Altimeter data
geom_line(aes(y = altimeter),
linetype = 2,
linewidth = 1,
color = altimeter_color) +
geom_point(y = altimeter, size = 3, color = altimeter_color) +
#Slope kilometers data
geom_line(aes(y = slope_kilometers),
linetype = 2,
linewidth = 1,
color = slope_kilometers_color) +
geom_point(y = slope_kilometers, size = 3, color = slope_kilometers_color) +
#Y-Axis
scale_y_break( c(9, 20), scales = 1.5) +
#Label
labs(x = "Tage",
y = "[km]") +
#Legend
scale_color_manual(values = c(altimeter_color, slope_kilometers_color)) +
#Title
ggtitle("Höhenmeter und Pistenkilometer meines 5-tägigen Skiurlaubs")
I tried different versions of scale_color_manual, labs, aes(fill="")
Update: I tweaked the former plot (removed):
One way to achieve what you want is:
First bring data in long format then put color inside aesthetics:
Rule of thumb: What is in aesthetics will have a legend:
library(tidyverse)
library(ggbreak)
df2 %>%
pivot_longer(-day) %>%
ggplot(aes(x = day)) +
#Altimeter data
geom_line(data = . %>% filter(name == "altimeter"), aes(y = altimeter, color = name),
linetype = 2, linewidth = 1 ) +
geom_point(data = . %>% filter(name == "altimeter"), aes(y = altimeter, color = name), size = 3) +
#Slope kilometers data
geom_line(data = . %>% filter(name == "slope_kilometers"), aes(y = slope_kilometers, color = name),
linetype = 2, linewidth = 1) +
geom_point(data = . %>% filter(name == "slope_kilometers"), aes(y = slope_kilometers, color = name), size = 3) +
#Y-Axis
scale_y_break( c(9, 20), scales = 1.5) +
#Label
labs(x = "Tage", y = "[km]", color = "") +
#Legend
scale_color_manual(values = c(altimeter_color, slope_kilometers_color)) +
#Title
ggtitle("Höhenmeter und Pistenkilometer meines 5-tägigen Skiurlaubs") +
theme(legend.position = "bottom")
I'm creating a simple point and line plot in ggplot similar to the example below:
dat <- iris %>% # dummy data
select(Sepal.Length) %>%
mutate(Type = "Sepal.Length")
ggplot() +
geom_point(data = dat, aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), colour = "orange") +
scale_shape_manual(values = 10) +
geom_hline(aes(yintercept = 6, linetype = 'C Red line'), colour = "red", size = 0.5) +
geom_hline(aes(yintercept = 5, linetype = 'A Blue line'), colour = "darkblue", size = 0.5) +
geom_hline(aes(yintercept = 7, linetype = 'B Green line'), colour = "forestgreen", size = 0.5) +
scale_linetype_manual(values = c('solid', 'dashed', 'dotted')) +
labs(linetype = "Line legend") +
labs(shape = "Point legend")
I'ev worked out that the alphabetical order of the 'names' associated with each line controls the order in the legend and I can match a desired line style to those lines using scale_linetype_manual. However, I cannot work out how to get the colours of the lines on the plot to be matched by the colours in linetype legend, which just uses the last specifed line colour?
Here is what I would do.
Instead of 3 individual geom_hline calls, let ggplot2 automatically plot horizontal lines based on data from a vert_data data.frame.
Combine legends for linetype and colour.
vert_data <- data.frame(
yintercept = c(6, 5, 7),
name = c("C Red line", "A Blue line", "B Green line"),
linetype = c("dotted", "solid", "dashed"),
colour = c("red", "darkblue", "forestgreen"))
ggplot() +
geom_point(
data = dat,
aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type),
colour = "orange") +
scale_shape_manual(values = 10) +
geom_hline(
data = vert_data,
aes(yintercept = yintercept, linetype = name, colour = name),
size = 0.5) +
scale_linetype_manual(
"Line legend", values = setNames(vert_data$linetype, vert_data$name)) +
scale_colour_manual(
"Line legend", values = setNames(vert_data$colour, vert_data$name)) +
labs(shape = "Point legend")
So I create a boxplot of data and then add a set point over that data. I want my legend to capture what the data type of the geom_points represents. Thanks!
ggplot(data = NULL) +
geom_boxplot(data = discuss_impact_by_county,
aes(x=reorder(State,discuss, FUN = median),y=discuss),
outlier.shape = NA) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
labs(x = "States") +
geom_point(data = by_state,
aes(x = State, y = discuss_happen_difference),
col = "red",
size = 3,
show.legend = TRUE)
If you want a legend you have to map on aesthetics. In your case map something on the color aes, i.e. move col="red" into aes() and use scale_color_manual to set the value and the legend label to be assgined to the color label "red".
As you have only one "category" of points you can simply do scale_color_manual(values = "red", label = "We are red points") to set the color and label. In case that your have multiple points with different colors it's best to make use of a named vector to assign the colors and legend labels to the right "color label"s, i.e use scale_color_manual(values = c(red = "red"), label = c(red = "We are red points")).
Using some random example data try this:
library(ggplot2)
library(dplyr)
set.seed(42)
discuss_impact_by_county <- data.frame(
State = sample(LETTERS[1:4], 100, replace = TRUE),
discuss = runif(100, 1, 5)
)
by_state <- discuss_impact_by_county %>%
group_by(State) %>%
summarise(discuss_happen_difference = mean(discuss))
#> `summarise()` ungrouping output (override with `.groups` argument)
ggplot(data = NULL) +
geom_boxplot(data = discuss_impact_by_county,
aes(x=reorder(State,discuss, FUN = median),y=discuss),
outlier.shape = NA) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
labs(x = "States") +
geom_point(data = by_state,
aes(x = State, y = discuss_happen_difference, col = "red_points"),
size = 3,
show.legend = TRUE) +
scale_color_manual(values = "red", label = "We are red points")
I am trying to define the legends of my plot in R.
I have the following code, this is a demo only, real data will have 7-8 columns and up to 20 samples
library(ggplot2)
library(RColorBrewer)
colors <-brewer.pal(n = 3, name = 'Paired')
ids <- c("TestA", "TestB", "TestC")
bg <-c(23, 13, 15)
sample1 <- c(21,15,17)
sample2 <- c(27,25,11)
sample3 <- c(24,14,18)
df <- data.frame(ids, bg, sample1,sample2,sample3)
ggplot(df) +
geom_col(aes(x = ids, y = bg), size = 1, color = "grey", fill = "grey") +
geom_point(aes(x = ids, y = sample1), size = 10, color=colors[1], group = 1) +
geom_point(aes(x = ids, y = sample2), size = 10, color=colors[2], group = 1) +
geom_point(aes(x = ids, y = sample3), size = 10, color=colors[3], group = 1)+
ggtitle("Plot title") +
xlab("x label") + ylab(" y label") +
scale_colour_manual(values = c("95% PI"= "black",
"Forecasts" = "red",
"Threshold" = "green"))
It produces the following output
How can I add legends like the following picture, and ensure that the color matches, i.e. legend color matches sample or bg color
Is this something like what you want?
ggplot(df) +
geom_col(aes(x = ids, y = bg, fill = "background"), size = 1, color = "grey") +
geom_point(data = df %>% pivot_longer(cols = starts_with("sample"), names_to = "sample", values_to = "values"), aes(x = ids, y = values, color = sample), size = 10) +
ggtitle("Plot title") +
xlab("x label") + ylab(" y label") +
scale_colour_manual(name = NULL, values = c("sample1"= "black",
"sample2" = "red",
"sample3" = "green"),
labels = c("95% PI", "forecasts", "Threshold")) +
scale_fill_manual(name = NULL, values = c("background" = "grey"))
I use bars and line to create my plot. The demo code is:
timestamp <- seq(as.Date('2010-01-01'),as.Date('2011-12-01'),by="1 mon")
data1 <- rnorm(length(timestamp), 3000, 30)
data2 <- rnorm(length(timestamp), 30, 3)
df <- data.frame(timestamp, data1, data2)
p <- ggplot()
p <- p + geom_histogram(data=df,aes(timestamp,data1),colour="black",stat="Identity",bindwidth=10)
p <- p + geom_line(data=df,aes(timestamp,y=data2*150),colour="red")
p <- p + scale_y_continuous(sec.axis = sec_axis(~./150, name = "data2"))
p <- p + scale_colour_manual(name="Parameter", labels=c("data1", "data2"), values = c('black', 'red'))
p <- p+ scale_shape_manual(name="Parameter", labels=c("data1", "data2"), values = c(15,95))
p
This results in a plot like this:
This figure does not have a legend. I followed this answer to create a customized legend but it is not working in my case. I want a square and line shape in my legend corresponding to bars and line. How can we get it?
I want legend as shown in below image:
For the type of data you want to display, geom_bar is a better fit then geom_histogram. When you to manipulate the appaerance of the legend(s), you need to place the colour = ... parts inside the aes. To get the desired result it probably best to use different types of legend for the line and the bars. In that way you are better able to change the appearance of the legends with guide_legend and override.aes.
A proposal for your problem:
ggplot(data = df) +
geom_bar(aes(x = timestamp, y = data1, colour = "black"),
stat = "Identity", fill = NA) +
geom_line(aes(x = timestamp, y = data2*150, linetype = "red"), colour = "red", size = 1) +
scale_y_continuous(sec.axis = sec_axis(~./150, name = "data2")) +
scale_linetype_manual(labels = "data2", values = "solid") +
scale_colour_manual(name = "Parameter\n", labels = "data1", values = "black") +
guides(colour = guide_legend(override.aes = list(colour = "black", size = 1),
order = 1),
linetype = guide_legend(title = NULL,
override.aes = list(linetype = "solid",
colour = "red",
size = 1),
order = 2)) +
theme_minimal() +
theme(legend.key = element_rect(fill = "white", colour = NA),
legend.spacing = unit(0, "lines"))
which gives: