I have a problem where the legend of my ggplot() does not appear. Here's my code:
plot_bt <- ggplot(NULL, aes(x, v1)) +
geom_line(data = nig_bt_1, colour = "black") +
geom_line(data = nig_bt_2, colour = "blue") +
geom_line(data = nig_bt_3, colour = "red") +
labs(x = "X", y = "Probability")
I tried to make a legend inside this graph but I could not do it. It just does not appear. I try to make a plot of three different types of NIG distribution. In nig_bt_1 etc. I have my values. Those three densities appear but the legend doesn't. I tried the scale_color_manual function too with no success.
Thank you very much.
x <- seq(-7.5,7.5,0.001)
nig_bt_1 <- data.frame(x ,v1 = dnig(x, param = pr_bt_1))
nig_bt_2 <- data.frame(x ,v1 = dnig(x, param = pr_bt_2))
nig_bt_3 <- data.frame(x ,v1 = dnig(x, param = pr_bt_3))
Just do this:
plot_bt <- ggplot(NULL, aes(x, v1)) +
geom_line(data = nig_bt_1, aes(colour = "a")) +
geom_line(data = nig_bt_2, aes(colour = "b")) +
geom_line(data = nig_bt_3, aes(colour = "c")) +
labs(x = "X", y = "Probability") +
scales_color_manual(values= c("a" = "black", "b" = "blue", "c" = "red"))
A guide can only depict mappings you've defined using aes. The ggplot2 way is of course to first combine the data and use a grouping variable.
Related
With the data separated by categories (Samples A and B), 2 layers were made, one for points and one for lines. I want to separate my data by category indicating colors for the points and also separate the lines but with different colors than those used for the points.
library(ggplot2)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
geom_line(aes(color = Sample)) +
scale_color_manual(values = c("red", "blue")) + #for poits
scale_color_manual(values = c("orange", "purple")) #for lines
Making use of the ggnewscale package this could be achieved like so:
library(ggplot2)
library(ggnewscale)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
scale_color_manual(name = "points", values = c("red", "blue")) + #for poits
new_scale_color() +
geom_line(aes(color = Sample)) +
scale_color_manual(name = "lines", values = c("orange", "purple")) #for lines
Using colour columns and scale_color_identity:
df$myCol1 <- rep(c("red", "blue"), each = 3)
df$myCol2 <- rep(c("orange", "purple"), each = 3)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = myCol1)) +
geom_line(aes(color = myCol2)) +
scale_color_identity()
I have the dataframe below:
etf_id<-c("a","b","c","d","e","a","b","c","d","e","a","b","c","d","e")
factor<-c("A","A","A","A","A","B","B","B","B","B","C","C","C","C","C")
normalized<-c(-0.048436801,2.850578601,1.551666490,0.928625186,-0.638111793,
-0.540615895,-0.501691539,-1.099239823,-0.040736139,-0.192048665,
0.198915407,-0.092525810,0.214317734,0.550478998,0.024613778)
df<-data.frame(etf_id,factor,normalized)
and I create a ggplotly() boxplot with:
library(ggplot2)
library(plotly)
ggplotly(ggplot(data = df, aes(x = factor, y = normalized)) +
geom_boxplot(aes(fill = as.factor(factor)),outlier.colour = 'black') +
geom_point(data = df, position = position_dodge(0.75))+geom_point(data = df,
aes(x = factor, y = normalized, shape = etf_id, color = etf_id),
size = 2))
I take as a result a boxplot with this legend:
but I want my legend to have only the color distinction like below. Note that the factors wont be 3 every time but may vary from 1 to 8.
The recommended way to alter plotly elements is to use the style() function. You can identify the elements and traces by inspecting plotly_json().
I'm not sure if there's a more compact way, but you can achieve the desired result using:
p <- ggplotly(ggplot(data = df, aes(x = factor, y = normalized)) +
geom_boxplot(aes(fill = as.factor(factor)),outlier.colour = 'black') +
geom_point(data = df, position = position_dodge(0.75))+geom_point(data = df,
aes(x = factor, y = normalized, shape = etf_id, color = etf_id),
size = 2))
p <- style(p, showlegend = FALSE, traces = 5:9)
for (i in seq_along(levels(df$factor))) {
p <- style(p, name = levels(df$factor)[i], traces = i)
}
p
Note that in this case the factor levels and traces align but that won't always be the case so you may need to adjust this (i.e. i + x).
One quick way would be to add show.legend = FALSE to supress the legend from showing.
library(ggplot2)
ggplot(data = df, aes(x = factor, y = normalized)) +
geom_boxplot(aes(fill = as.factor(factor)),outlier.colour = 'black') +
geom_point(position = position_dodge(0.75)) +
geom_point(aes(x = factor, y = normalized, shape = etf_id, color = etf_id),
size = 2, show.legend=FALSE)
Unfortunately, this does not work when this is passed to ggplotly. You can use theme(legend.position='none') which works but suppresses all the legends instead of specific ones. One dirty hack is to disable specific legend manually
temp_plot <- ggplotly(ggplot(data = df, aes(x = factor, y = normalized)) +
geom_boxplot(aes(fill = as.factor(factor)),outlier.colour = 'black') +
geom_point(position = position_dodge(0.75)) +
geom_point(aes(x = factor, y = normalized, shape = etf_id, color = etf_id),size = 2))
temp_plot[[1]][[1]][4:9] <- lapply(temp_plot[[1]][[1]][4:9], function(x) {x$showlegend <- FALSE;x})
temp_plot
Steeling the example of this question (Link), I want to ask if it is possible to add the additional blue point to the legend?
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
g1 <- dat[15,]
ggplot(dat, aes(x = xvar, y = yvar, shape = cond,
colour = cond), size = 2.5) +
geom_point(alpha = 1) +
geom_point(data = g1, colour = "blue", size = 4, show_guide = FALSE)
You can put the aesthetics for the additional points layer inside aes instead of outside to get it added to the legend. You can use any string value; that string will be the name in the legend.
Then you can control the color and shape of that point via scale_*_manual layers.
I additionally changed the size of that point in the legend using override.aes, which is optional.
ggplot(dat, aes(x = xvar, y = yvar, shape = cond,
colour = cond), size = 2.5) +
geom_point(alpha = 1) +
geom_point(data = g1, aes(colour = "Point 15", shape = "Point 15"), size = 4) +
scale_shape_manual(values = c(16, 17, 17) ) +
scale_color_manual(values = c("pink", "turquoise", "blue") ) +
guides(color = guide_legend( override.aes = list(size = c(1.5, 1.5, 4) ) ) )
You probably have to change the condition of that point in the data as in your example or add it to the date, if it is not already part of it.
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
dat$size = 2.5
dat[15,]$cond = "C"
dat$cond = as.character(dat$cond)
dat[15,]$size = 4
ggplot(dat, aes(x = xvar, y = yvar, shape = cond,
colour = cond, size=size)) +
geom_point(alpha = 1) +
scale_colour_manual(values=c("red", "turquoise", "blue")) +
scale_size_continuous(guide = FALSE)
I am doing a plot of densities, I want to add a legend but is overlapped with the symbol. The code is hereunder:
dfGamma = data.frame(a = rgamma(100,shape = 7.1,rate= 0.0055),
b = rgamma(100, shape = 10,rate= 0.0055),
c = rgamma(100, shape = 7.1,rate= 0.0055))
dfGamma = stack(dfGamma)
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, colour = ind),position="identity",geom="line",size=1)+
ggtitle("Gamma distribution")+theme(legend.position="right")+
scale_color_manual(labels = c(expression(paste(alpha,"=7.1 ",beta,"=0.0055")),
expression(paste(alpha,"= 10 ",beta,"=0.0055")),
expression(paste(alpha,"=7.1 ",beta,"=0.0055"))),
values = c('red', 'blue',"green"))
p
the plot is:
The guides option, guide_legend is what you need. You can read more about it in the ggplot reference. Does this help?
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, colour = ind),position="identity",geom="line",size=1)+
ggtitle("Gamma distribution")+
theme(legend.position="right") +
scale_color_manual(labels = c(expression(paste(alpha, "=7.1 ", beta, "=0.0055")),
expression(paste(alpha,"= 10 ",beta,"=0.0055")),
expression(paste(alpha,"=7.1 ",beta,"=0.0055"))),
values = c('red', 'blue',"green")) +
guides(colour = guide_legend(label.position = "bottom"))
p
I'm trying to get a legend to show up in a left chart I've developed. It's not showing up. I've checked ggplot legend not working with scale_colour_manual as well as How to add a legend in ggplot (not showing up) to no avail.
Here is some sample data:
#x axis values
deciles <- c(1:10)
#model_1
decile_act_pp <- c(393.6773, 243.0795, 250.2033, 220.0076, 180.7292,
187.3803,208.8504,162.9708,140.9405,107.7656)
#model_2
model2_pp_norm <- c(537.9617, 306.0807, 244.6228, 207.8051, 181.8801,
161.3744,142.8224,125.3262,107.5905, 80.13438)
#model_3
model1_pp_norm <- c(515.9927,297.8425, 240.8723, 206.6129, 183.6805,
164.3337, 148.4509,134.1227, 115.0055, 88.68549)
#combine to make a chart
df <- as.data.frame(cbind(deciles, decile_act_pp, model2_pp_norm,
model1_pp_norm))
#develop the chart
ggplot(data = df, aes(x = as.factor(deciles), group = 1)) +
geom_point(aes(y = decile_act_pp), color = "blue") +
geom_line(aes(y = decile_act_pp), color = "blue") +
geom_point(aes(y = model2_pp_norm), color = "red") +
geom_line(aes(y = model2_pp_norm), color = "red") +
geom_point(aes(y = model1_pp_norm), color = "green") +
geom_line(aes(y = model1_pp_norm), color = "green") +
xlab("Deciles") +
labs(colour="Datasets",x="Deciles",y="Pure Premium") +
scale_color_manual('', limits = c('decile_act_pp', 'model2_pp_norm',
'model1_pp_norm'), values = c("blue", "red", "green"))
The chart look exactly as I want it minus missing the legend. Can anyone tell me what I'm doing wrong?
library(reshape2)
df2 <- melt(data = df, id.vars = 1)
ggplot(data = df2, aes(x = as.factor(deciles), group = 1)) + geom_point(aes( y=value, color = variable)) + geom_line(aes(y = value, group = variable, color = variable))