in the following sample data , how can i display the abline ( i e the red color line) in legend with y.
my code and data:
x<-c(1990,1991,1992,1993,1994,1995)
y<-c(400,500,465,450,550,555)
df<-data.frame(x,y)
df
plot1<- ggplot(df, aes(x)) +
geom_line(size=0.5,lty="dashed", aes(y=y),color="Blue") +
geom_abline(aes(slope=-0.62,intercept=1670,colour="break"),size=0.9)+
geom_point(aes(y=y,shape="y"),size=4, color="Gray24",fill="Green")
plot1
.
what i got is the below image. i need the red line to be displayed in legend
You can use the show_guide=TRUE argument:
plot1<- ggplot(df, aes(x)) +
geom_line(size=0.5,lty="dashed", aes(y=y),color="Blue") +
geom_abline(aes(slope=-0.62,intercept=1670,colour="break"), size=0.9,show_guide = TRUE)+
geom_point(aes(y=y,shape="y"),size=4, color="Gray24",fill="Green")
You'll probably need to change the labels in the legend, but you should be able to do that with theme.
Edit: To remove the slash from the legend, you can use guides and override.aes:
plot1 <- ggplot(df, aes(x, y)) +
geom_point(aes(shape = "y"), size = 4, color = "Gray24", lty = 0) +
geom_line(size = 0.5, lty = "dashed", color = "Blue") +
geom_abline(aes(slope = -0.62, intercept = 1670, colour = "break"), size = 0.9,
show_guide = TRUE) +
guides(shape = guide_legend(override.aes = list(linetype = 0)))
Related
I have a dataset containing 1,000 values for a model, these values are all within the same range (y=40-70), so the points overlap a ton. I'm interested in using color to show the density of the points converging on a single value (y=56.72) which I have indicated with a horizontal dashed line on the plot below. How can I color these points to show this?
ggplot(data, aes(x=model, y=value))+
geom_point(size=1) +
geom_hline(yintercept=56.72,
linetype="dashed",
color = "black")
I think that you should opt for an histogram or density plot:
n <- 500
data <- data.frame(model= rep("model",n),value = rnorm(n,56.72,10))
ggplot(data, aes(x = value, y = after_stat(count))) +
geom_histogram(binwidth = 1)+
geom_density(size = 1)+
geom_vline(xintercept = 56.72, linetype = "dashed", color = "black")+
theme_bw()
Here is your plot with the same data:
ggplot(data, aes(x = model, y = value))+
geom_point(size = 1) +
geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")
If your model is iterative and do converge to the value, I suggest you plot as a function of the iteration to show the convergence. An other option, keeping a similar plot to your, is dodging the position of the points :
ggplot(data, aes(x = model, y = value))+
geom_point(position = position_dodge2(width = 0.2),
shape = 1,
size = 2,
stroke = 1,
alpha = 0.5) +
geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")
Here is a color density plot as you asked:
library(dplyr)
library(ggplot2)
data %>%
mutate(bin = cut(value, breaks = 10:120)) %>%
dplyr::group_by(bin) %>%
mutate(density = dplyr::n()) %>%
ggplot(aes(x = model, y = value, color = density))+
geom_point(size = 1) +
geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")+
scale_colour_viridis_c(option = "A")
I would suggest to use the alpha parameter within the geom_point. You should use a value close to 0.
ggplot(data, aes(x=model, y=value)) +
geom_point(size=1, alpha = .1) +
geom_hline(yintercept=56.72, linetype="dashed", color = "black")
The following code block generates a plot with two legends:
Spend7d_bubble <- ggplot(cluster_visuals,
aes(x = ltv_7d, y = avg_daily_sessions,
color = factor(cluster8), size = n)) +
geom_point(alpha = 0.5) +
scale_size_continuous(range = c(2, 25))
Notice how this generates two legends on the right, one for n and one for factor(cluster8).
How can I only include the legend for factor(cluster8) and also rename it to just 'cluster'?
Spend7d_bubble <- ggplot(cluster_visuals,
aes(x = ltv_7d, y = avg_daily_sessions,
color = factor(cluster8), size = n)) +
geom_point(alpha = 0.5) +
scale_size_continuous(range = c(2, 25), guide = 'none') +
labs(color = "Cluster")
Whichever of those aesthetics (color or size) that you don't want a legend for, should be out of aes(). As you see, you don't have any legend for alpha in geom_point since it is not an argument of aes.
ggplot(cluster_visuals,
aes(x = ltv_7d, y = avg_daily_sessions, color = factor(cluster8)), size = n) +
geom_point(alpha = 0.5) +
scale_size_continuous(range = c(2, 25))
How to remove inclined lines added to legend? And also the dots on the yellow and gray... Why is it happening?
library(ggplot2)
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
df2 = data.frame(x=x, y=x^2-5*x-10)
ggplot(df, aes(x, y)) +
geom_point(aes(size = v)) +
theme_classic() +
scale_size("blabla") +
geom_point(data=df2, aes(x, y, color = "blue")) +
geom_line(data=df2, aes(x, y, color = "blue")) +
geom_hline(aes(color="gray",yintercept=25)) +
geom_abline(aes(color="yellow", intercept=0, slope=1)) +
scale_color_manual(values = c("blue","gray","yellow"), labels = c("nanana","hhh","abab"), name = "other")
That's the legend for the color aesthetic and it tries to combine all the needed information for geom_point, geom_line, geom_hline, and geom_abline. To get rid of the lines, we instead need
geom_abline(aes(color = "yellow", intercept = 0, slope = 1), show.legend = FALSE)
while for the dots we have to add
guides(color = guide_legend(override.aes = list(shape = c(19, NA, NA))))
This gives
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:
I have a plot with three different legends: one for linetype, one for color, and one for fill. In the color and fill legends there are also some lines which I wish to remove, but how?
Here is some example code:
# some data
hline_df <- data.frame(name = c('a', 'b'), y = c(1, 2))
df <- data.frame(x = c(1, 2), y = c(0.5, 1.5), con = c('a', 'b'), col = c('d', 'e'))
# the plot
ggplot(df, aes(x, y, fill = con)) +
geom_bar(stat = 'identity') +
geom_point(aes(color = col)) +
geom_hline(data = hline_df, aes(yintercept = y, linetype = name),
color = 'red', show_guide = TRUE)
I get the "name" guide for both red lines, that is fine.
The "col" guide has red lines crossing the dots, I want to remove them!
The "con" guide also has red lines which should be removed.
I could modify parts of the legend with
guides(fill = guide_legend(override.aes = list(colour = NULL)),
color = guide_legend(override.aes = list(colour = NULL)))
This removes the colour, but the lines are still there.
Thanks in advance!
You may set linetype = 0 or "blank" (on different linetypes here) for the filland color guides in your override.aes call.
Also note that I moved the fill aes from the 'top level' in ggplot to geom_bar.
ggplot(df, aes(x, y)) +
geom_bar(aes(fill = con), stat = 'identity') +
geom_point(aes(color = col)) +
geom_hline(data = hline_df, aes(yintercept = y, linetype = name), color = 'red', show_guide = TRUE) +
guides(fill = guide_legend(override.aes = list(linetype = 0)),
color = guide_legend(override.aes = list(linetype = 0)))
As suggested by user20650
ggplot(df, aes(x,y)) +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) +
geom_point(aes(color=col), size=5) +
geom_bar(aes(fill=con), stat='identity') +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F) +
guides(color = guide_legend(override.aes = list(linetype = 0)))
So the first geom_hline creates the legend but the line is behind the bars...
the second call brings the line in front of the bars but does not print a legend (great idea).
The las guide is overwriting the aesthetics line type with 0... In this way it removes the line from the legends... I tried with NULL but this didn't worked before...
Thanks again.
Using:
ggplot(df, aes(x,y,fill=con)) + geom_bar(stat='identity') +
geom_point(aes(color=col)) +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name),color='red',show_guide=FALSE) +
guides(linetype=FALSE,color=FALSE)
gives me this plot: