how to add legends ggplot in charts facet_warp() in R? - r

I maked the chart in R using gglopt() and facet_warp(), but do not appear legends of geom_lines() and stat_smooth().
my code exemple is:
p <- ggplot(data = mtcars, aes(x = hp, y = disp)) +
geom_line(color="red")+
facet_wrap(~cyl)+
stat_smooth()+
guides()
how to add legends in the chart final?

You can add the labels for color aesthetics for each plot and link the color using named vectors in values parameter of scale_color_manual().
ggplot(data = mtcars, aes(x = hp, y = disp)) +
geom_line(aes(color = "line.color")) +
stat_smooth(aes(color = "smooth.color")) +
facet_wrap(~cyl) +
scale_color_manual(name = "", values = c("line.color" = "red", "smooth.color" = "blue"))

Related

Can I manually change boxplot color with ggMarginal?

I'm using ggMarginal to make marginal boxplots. Is there a way to manually change the color and/or fill of the boxplots without a grouping variable? I'd like to have different colors on the x boxplot and the y boxplot.
library(tidyverse)
library(ggExtra)
foo <- data.frame(x=rnorm(100,mean=1,sd=1),
y=rnorm(100,mean=2,sd=2))
p1 <- ggplot(data = foo,aes(x=x,y=y)) +
geom_point() + coord_equal()
ggMarginal(p1, type="boxplot", size=12)
Provided I have understood you correctly, you can do the following
p1 <- ggplot(data = foo, aes(x = x, y = y)) +
geom_point() +
coord_equal()
ggMarginal(
p1,
type = "boxplot",
size = 12,
xparams = list(colour = "blue"),
yparams = list(colour = "red"))

Add Legend and change Colour on grouped bar plot

I created a plot like this;
library("ggplot2")
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = color, y = ..prop.., group = 2)) +
scale_y_continuous(labels=scales::percent) +
facet_grid(~cut)
Now I want to add a legend for the variable "color", also I want to change the colour of the bars. The graph is exactly how I want it to be, and if possible I don't want change the structure of the dataset, just add a legend and change colours.
I could not find example that fit for this "percentage"-style graphics.
ggplot(data = diamonds, aes(x = color, y = ..prop.., group = cut)) +
geom_bar(aes(fill = factor(..x.., labels = LETTERS[seq(from = 4, to = 10 )]))) +
labs(fill = "color") +
scale_y_continuous(labels = scales::percent) +
facet_grid(~ cut)

Plot multiple group histogram with overlaid line ggplot

I'm trying to plot a multiple group histogram with overlaid line, but I cannot get the right scaling for the histogram.
For example:
ggplot() + geom_histogram(data=df8,aes(x=log(Y),y=..density..),binwidth=0.15,colour='black') +
geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw()
produces the right scale. But when I try to perform fill according to groups, each group is scaled separately.
ggplot() + geom_histogram(data=df8,aes(x=log(Y),fill=vec8,y=..density..),binwidth=0.15,colour='black') +
geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw()
How would I scale it so that a black line is overlaid over the histogram and on the y axis is density?
It is going to be difficult for others to help you without a reproducible example, but perhaps something like this is what you're after:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg, fill = factor(cyl))) +
geom_histogram(aes(y = ..density..)) +
geom_line(stat = "density")
If you would rather the density line pertain to the entire dataset, you need to move the fill aesthetic into the geom_histogram function:
ggplot(data = mtcars, aes(x = mpg)) +
geom_histogram(aes(y = ..density.., fill = factor(cyl))) +
geom_line(data = mtcars, stat = "density")

Overlay lines and hist with ggplot2

I'm creating a table with a lot of plot using ggplot
a=rnorm(30)
b=a*a
c=rnorm(30)
d=c
l=runif(30)
m=l+3
data=data.frame(A=a,B=b,ss=1)
data=rbind(data,data.frame(A=c,B=d,ss=2))
ggplot()+ geom_line(data=data,aes(A,B,group=ss),col="red")+facet_wrap(~ ss,as.table=T
In each of this plots I have to overlap an histogram.
How can I do?
Here's a way to do it:
ggplot() +
geom_line(data = data, aes(x = A, y = B), col = "red") +
geom_histogram(data = data, aes(x = A), alpha = .5) +
facet_wrap(~ ss,as.table=T)

Adding legend to a single plot of multiple linear regression plot

I plotted two ggplots from two different datasets in one single plot. plots are simple linear regression. I want to add legend both for lines and dots in the plot with different colours. How can I do that? The code I used for plot is as below. But, I failed to add a desirable legend to that.
ggplot() +
geom_point(aes(x = Time_1, y = value1)) +
geom_point(aes(x = Time_2, y = value2)) +
geom_line(aes(x = Time_1, y = predict(reg, newdata = dataset)))+
geom_line(aes(x = Time_Month.x, y = predict(regressor, newdata = training_set)))+
ggtitle('Two plots in a single plot')
ggplot2 adds legends automatically if it has groups within the data. Your original code provides the minimum amount of information to ggplot(), basically enough for it to work but not enough to create a legend.
Since your data comes from two different objects due to the two different regressions, then it looks like all you need in this case is to add the 'color = "INSERT COLOR NAME"' argument to each geom_point() and each geom_line(). Using R's built-in mtcars data set for example, what you have is similar to
ggplot(mtcars) + geom_point(aes(x = cyl, y = mpg)) + geom_point(aes(x = cyl, y = wt)) + ggtitle("Example Graph")
Graph without Legend
And what you want can be obtained by using something similar to,
ggplot(mtcars) + geom_point(aes(x = cyl, y = mpg, color = "blue")) + geom_point(aes(x = cyl, y = wt, color = "green")) + ggtitle("Example Graph")
Graph with Legend
Which would seem to translate to
ggplot() +
geom_point(aes(x = Time_1, y = value1, color = "blue")) +
geom_point(aes(x = Time_2, y = value2, color = "green")) +
geom_line(aes(x = Time_1, y = predict(reg, newdata = dataset), color = "red"))+
geom_line(aes(x = Time_Month.x, y = predict(regressor, newdata = training_set), color = "yellow"))+
ggtitle('Two plots in a single plot')
You could also use the size, shape, or alpha arguments inside of aes() to differentiate the different series.

Resources