I want to add the legend next to my plot in order to be able to show which color is explaining each variable. How can I do that? Here's my code:
scatter = ggplot(vehicles, aes(x = seq(1,108))) +
geom_point(aes(y = RV),size = 2,color = "blue" ) +
geom_point(aes(y= BOP), size = 2, color = "red") +
scatter
I just obtained my plot but without the names in it
It would be clearer if you can provide the vehicles data as well but I'm guessing this would work:
scatter = ggplot(vehicles, aes(x = seq(1,108))) +
geom_point(aes(y = RV, colour = "RV"),size = 2 ) +
geom_point(aes(y= BOP, colour = "BOP"), size = 2) +
scatter
Related
I am using ggplot to plot multiple dataframes in one plot.
ggplot(data2_CY, aes(x = Date, y = AVERAGETOTALCOST)) + geom_point(color = 'blue') + geom_point(data = data2_CN, color = 'red') +
geom_point(data = data2_LY, color = 'cyan') + geom_point(data = data2_LN, color = 'magenta') +
labs(title = "Trends Over Time", x = "Time", y = "Average Total Cost") + scale_x_date(breaks = seq(as.Date("2015-01-01"), as.Date("2019-07-01"), by = "6 months"), date_labels = "%b\n%Y") +
scale_color_manual(values = c('C&Y' = 'blue', 'C&N' = 'red', 'L&Y' = 'cyan', 'L&N' = 'magenta'))
The code above returns a singular plot with multiple points in the correct colors. However, the scale_color_manual() portion of the code does not return a legend. I am trying to display the legend to the right of the graph.
Thanks for the help!
The scale_color_manual() must connect to a specific data. My advice to you is to merge all these data and plot them using one dataframe
I have a chart that shows mobile usage by operating system. I'd like to add vertical lines to identify when those operating systems were released. I'll go through the chart and then the code.
The chart -
The code -
dev %>%
group_by(os) %>%
mutate(monthly_change = prop - lag(prop)) %>%
ggplot(aes(month, monthly_change, color = os)) +
geom_line() +
geom_vline(xintercept = as.numeric(ymd("2013-10-01"))) +
geom_text(label = "KitKat", x = as.numeric(ymd("2013-10-01")) + 80, y = -.5)
Instead of adding the text in the plot, I'd like to create a legend to identify each of the lines. I'd like to give each of them its own color and then have a legend to identify each. Something like this -
Can I make my own custom legend like that?
1) Define a data frame that contains the line data and then use geom_vline with it. Note that BOD is a data frame that comes with R.
line.data <- data.frame(xintercept = c(2, 4), Lines = c("lower", "upper"),
color = c("red", "blue"), stringsAsFactors = FALSE)
ggplot(BOD, aes( Time, demand ) ) +
geom_point() +
geom_vline(aes(xintercept = xintercept, color = Lines), line.data, size = 1) +
scale_colour_manual(values = line.data$color)
2) Alternately put the labels right on the plot itself to avoid an extra legend. Using the line.data frame above. This also has the advantage of avoiding possible multiple legends with the same aesthetic.
ggplot(BOD, aes( Time, demand ) ) +
geom_point() +
annotate("text", line.data$xintercept, max(BOD$demand), hjust = -.25,
label = line.data$Lines) +
geom_vline(aes(xintercept = xintercept), line.data, size = 1)
3) If the real problem is that you want two color legends then there are two packages that can help.
3a) ggnewscale Any color geom that appears after invoking new_scale_color will get its own scale.
library(ggnewscale)
BOD$g <- gl(2, 3, labels = c("group1", "group2"))
line.data <- data.frame(xintercept = c(2, 4), Lines = c("lower", "upper"),
color = c("red", "blue"), stringsAsFactors = FALSE)
ggplot(BOD, aes( Time, demand ) ) +
geom_point(aes(colour = g)) +
scale_colour_manual(values = c("red", "orange")) +
new_scale_color() +
geom_vline(aes(xintercept = xintercept, colour = line.data$color), line.data,
size = 1) +
scale_colour_manual(values = line.data$color)
3b) relayer The experimental relayer package (only on github) allows one to define two color aethetics, color and color2, say, and then have separate scales for each one.
library(dplyr)
library(relayer)
BOD$g <- gl(2, 3, labels = c("group1", "group2"))
ggplot(BOD, aes( Time, demand ) ) +
geom_point(aes(colour = g)) +
geom_vline(aes(xintercept = xintercept, colour2 = line.data$color), line.data,
size = 1) %>% rename_geom_aes(new_aes = c("colour" = "colour2")) +
scale_colour_manual(aesthetics = "colour", values = c("red", "orange")) +
scale_colour_manual(aesthetics = "colour2", values = line.data$color)
You can definitely make your own custom legend, but it is a bit complicated, so I'll take you through it step-by-step with some fake data.
The fake data contained 100 samples from a normal distribution (monthly_change for your data), 5 groupings (similar to the os variable in your data) and a sequence of dates from a random starting point.
library(tidyverse)
library(lubridate)
y <- rnorm(100)
df <- tibble(y) %>%
mutate(os = factor(rep_len(1:5, 100)),
date = seq(from = ymd('2013-01-01'), by = 1, length.out = 100))
You already use the colour aes for your call to geom_line, so you will need to choose a different aes to map onto the calls to geom_vline. Here, I use linetype and a call to scale_linetype_manual to manually edit the linetype legend to how I want it.
ggplot(df, aes(x = date, y = y, colour = os)) +
geom_line() +
# set `xintercept` to your date and `linetype` to the name of the os which starts
# at that date in your `aes` call; set colour outside of the `aes`
geom_vline(aes(xintercept = min(date),
linetype = 'os 1'), colour = 'red') +
geom_vline(aes(xintercept = median(date),
linetype = 'os 2'), colour = 'blue') +
# in the call to `scale_linetype_manual`, `name` will be the legend title;
# set `values` to 1 for each os to force a solid vertical line;
# use `guide_legend` and `override.aes` to change the colour of the lines in the
# legend to match the colours in the calls to `geom_vline`
scale_linetype_manual(name = 'lines',
values = c('os 1' = 1,
'os 2' = 1),
guide = guide_legend(override.aes = list(colour = c('red',
'blue'))))
And there you go, a nice custom legend. Please do remember next time that if you can provide your data, or a minimally reproducible example, we can better answer your question without having to generate fake data.
I am trying to add a legend to ggplot2 based on some horizontal comparison lines that were added in. My code currently looks like this
aplot <- ggplot(aData, aes(x = DEP, y = DECAY, color = GENDER))
aplot +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_hline(yintercept = meanCD, color = "purple")+
geom_hline(yintercept = medianCD, color = "forestgreen") +
geom_hline(yintercept = medianSAD, color = "goldenrod3", linetype = "dashed")+
geom_hline(yintercept = meanSAD, color = "deeppink", linetype = "dashed")
meanCD, medianCD, meanSD, medianSD are all stored as seperate values and I need to add them to the graph for comparitive purposes. aData is just a bunch of points. I cannot get a legend to show up giving the color of the line and giving an appropriate label and I am unsure how to accomplish this in ggplot2.
I'm creating several heatmaps using the following code in r .
for(i in 1:length(pdi_levels)) {
p <- ggplot(pdi_tasa_mean_lps_filter %>% filter(inst_finan == pdi_levels[i]),
aes(x = emision_mes, y = institucion)) +
geom_point(size = 10, aes(colour = porcentaje)) +
geom_text(aes(x = emision_mes, y = institucion, label = porcentaje), color = "white", size = 2) +
theme_bw() +
facet_wrap(~emision_anio, scales = "free") +
scale_color_gradient(low = "pink", high = "red")
print(p)
}
Here is an example of my two heat maps.
My problem here is that the points(circles) overlap one another, however, if I were to assign to the size argument in the function geom_point()
geom_point(size = X, aes(colour = porcentaje))
the distance between each main axis, the circles would fit perfectly.
my questions being:
How do I obtain the distance between each main axis?
Is there any other way to make the circles NOT overlap?
the corrplot package is NOT an option.
I would like to apply the scale colour gradient also to the smooth line.
At the moment the code below set the color fix to red.
library(ggplot2)
a <- data.frame(year = 1:100, values = sin(1:100)*1000 + runif(100))
ggplot(a, aes(x = year, y = values, color = values )) + geom_line(size = 2) +
scale_colour_gradient2(
low = "blue",
mid = "white" ,
high = "red",
midpoint = 10
)+
geom_smooth(
data = a,
aes(x = year, y = values),
color = "red",
size = 2
)
But when I set color = values it doesn't work. Instead it takes the default blue.
geom_smooth(
data = a,
aes(x = year, y = values, color = values),
size = 2
)
Thanks in advance.
Use geom_smooth(aes(color=..y..)) to add a color aesthetic to geom_smooth. ..y.. is the vector of y-values internally calculated by geom_smooth to create the regression curve. In general, when you want to add an aesthetic to a summary value that's calculated internally, you need to map the aesthetic to that internal value. Here, the internal value is the ..y.. value of the smoothing function. In other cases it might be ..count.. for histograms or bar plots, or ..density.. for density plots.
Here's an example using your data. Note that I've tweaked a few of the plot parameters for illustration.
set.seed(48)
a <- data.frame(year = 1:100, values = sin(1:100)*1000 + runif(100))
ggplot(a, aes(x = year, y = values, color = values )) +
geom_line(size = 0.5) +
geom_smooth(aes(color=..y..), size=1.5, se=FALSE) +
scale_colour_gradient2(low = "blue", mid = "yellow" , high = "red",
midpoint=10) +
theme_bw()
Note that the color of the regression line does not change much because its y-values span a small range relative to the data. Here's another example with fake data that generates a more wide-ranging regression curve.
set.seed(1938)
a2 <- data.frame(year = seq(0,100,length.out=1000), values = cumsum(rnorm(1000)))
ggplot(a2, aes(x = year, y = values, color = values )) +
geom_line(size = 0.5) +
geom_smooth(aes(color=..y..), size=1.5, se=FALSE) +
scale_colour_gradient2(low = "blue", mid = "yellow" , high = "red",
midpoint=median(a2$values)) +
theme_bw()