Add vertical lines at multiple datetimes of outlier points using ggplot2 - r

I have a dataframe like this
PDATETIME <- c("2017-02-23 06:08:39","2017-02-25 15:31:50","2017-03-06 17:11:57","2017-03-15 01:23:51",
"2017-03-16 15:54:35","2017-03-16 23:48:14","2017-03-18 02:57:41","2017-03-20 05:12:33")
DELTA <- c(2.5,8,3.5,4.5,5.5,8.3,3.3,4)
Type <- c(NA,"Outlier",NA,NA,NA,"Outlier",NA,NA)
df <- data.frame(PDATETIME,DELTA,Type)
df$PDATETIME <- as.POSIXct(df$PDATETIME,format="%Y-%m-%d %H:%M:%S")
I am trying to draw vertical lines at the outlier points using ggplot2
library(ggplot2)
library(ggrepel)
ggplot(data = df, aes(PDATETIME,DELTA ))+
ggtitle("Outlier Analysis") +
theme(axis.text.x = element_text(angle=90, vjust=1),plot.title = element_text(size = rel(1))) +
geom_point(colour="black") +
geom_vline(aes(xintercept=df$PDATETIME[which(df$Type %in% "Outlier")],linetype=4, colour="black")) +
geom_text_repel(aes(PDATETIME, DELTA,
label = Type),
size =4,
fontface = 'bold',
color = 'red',
box.padding = 0.5,
point.padding = 0.5,
segment.color = 'darkblue',
segment.size = 0.5,
arrow = arrow(length = unit(0.01, 'npc'))) +
xlab("PDATETIME")+
ylab("DELTA")
It throws an error "Error: A continuous variable can not be mapped to linetype"
The outlier points are at 2017-02-25 15:31:50, 2017-03-16 23:48:14
What am I missing here? Could someone point me in the right direction?

linetype and color are not varying so you can move it outside aes. Also I recommend that you modify your code to:
geom_vline(data = df[which(df$Type %in% "Outlier"),],
aes(xintercept = PDATETIME),
linetype = 4, colour = "black")

We don't need aes inside geom_vline, try:
geom_vline(xintercept = df$PDATETIME[ which(df$Type %in% "Outlier") ], linetype = 4, colour = "black")

You need to move linetype and colour out of aes:
geom_vline(aes(xintercept=df$PDATETIME[which(df$Type %in% "Outlier")]),linetype=4, colour="black")
You would only want linetype and/or colour inside of aes() if you want them to vary according to some variable, like df$type.

Related

Combine legend for fill and colour ggplot to give only single legend

I am plotting a smooth to my data using geom_smooth and using geom_ribbon to plot shaded confidence intervals for this smooth. No matter what I try I cannot get a single legend that represents both the smooth and the ribbon correctly, i.e I am wanting a single legend that has the correct colours and labels for both the smooth and the ribbon. I have tried using + guides(fill = FALSE), guides(colour = FALSE), I also read that giving both colour and fill the same label inside labs() should produce a single unified legend.
Any help would be much appreciated.
Note that I have also tried to reset the legend labels and colours using scale_colour_manual()
The below code produces the below figure. Note that there are two curves here that are essentially overlapping. The relabelling and setting couours has worked for the geom_smooth legend but not the geom_ribbon legend and I still have two legends showing which is not what I want.
ggplot(pred.dat, aes(x = age.x, y = fit, colour = tagged)) +
geom_smooth(size = 1.2) +
geom_ribbon(aes(ymin = lci, ymax = uci, fill = tagged), alpha = 0.2, colour = NA) +
theme_classic() +
labs(x = "Age (days since hatch)", y = "Body mass (g)", colour = "", fill = "") +
scale_colour_manual(labels = c("Untagged", "Tagged"), values = c("#3399FF", "#FF0033")) +
theme(axis.title.x = element_text(face = "bold", size = 14),
axis.title.y = element_text(face = "bold", size = 14),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
legend.text = element_text(size = 12))
The problem is that you provide new labels for the color-aesthetic but not for the fill-aesthetic. Consequently ggplot shows two legends because the labels are different.
You can either also provide the same labels for the fill-aesthetic (code option #1 below) or you can set the labels for the levels of your grouping variable ("tagged") before calling ggplot (code option #2).
library(ggplot2)
#make some data
x = seq(0,2*pi, by = 0.01)
pred.dat <- data.frame(x = c(x,x),
y = c(sin(x), cos(x)) + rnorm(length(x) * 2, 0, 1),
tag = rep(0:1, each = length(x)))
pred.dat$lci <- c(sin(x), cos(x)) - 0.4
pred.dat$uci <- c(sin(x), cos(x)) + 0.4
#option 1: set labels within ggplot call
pred.dat$tagged <- as.factor(pred.dat$tag)
ggplot(pred.dat, aes(x = x, y = y, color = tagged, fill = tagged)) +
geom_smooth(size = 1.2) +
geom_ribbon(aes(ymin = lci, ymax = uci), alpha = 0.2, color = NA) +
scale_color_manual(labels = c("untagged", "tagged"), values = c("#F8766D", "#00BFC4")) +
scale_fill_manual(labels = c("untagged", "tagged"), values = c("#F8766D", "#00BFC4")) +
theme_classic() + theme(legend.title = element_blank())
#option 2: set labels before ggplot call
pred.dat$tagged <- factor(pred.dat$tag, levels = 0:1, labels = c("untagged", "tagged"))
ggplot(pred.dat, aes(x = x, y = y, color = tagged, fill = tagged)) +
geom_smooth(size = 1.2) +
geom_ribbon(aes(ymin = lci, ymax = uci), alpha = 0.2, color = NA) +
theme_classic() + theme(legend.title = element_blank())

Adding legend for combo bar and line graph -- ggplot ignoring commands

I am trying to make a bar chart with line plots as well. The graph has created fine but the legend does not want to add the line plots to the legend.
I have tried so many different ways of adding these to the legend including:
ggplot Legend Bar and Line in Same Graph
None of which have worked. show.legend also seems to have been ignored in the geom_line aes.
My code to create the graph is as follows:
ggplot(first_q, aes(fill = Segments)) +
geom_bar(aes(x= Segments, y= number_of_new_customers), stat =
"identity") + theme(axis.text.x = element_blank()) +
scale_y_continuous(expand = c(0, 0), limits = c(0,3000)) +
ylab('Number of Customers') + xlab('Segments') +
ggtitle('Number Customers in Q1 by Segments') +theme(plot.title =
element_text(hjust = 0.5)) +
geom_line(aes(x= Segments, y=count) ,stat="identity",
group = 1, size = 1.5, colour = "darkred", alpha = 0.9, show.legend =
TRUE) +
geom_line(aes(x= Segments, y=bond_count)
,stat="identity", group = 1, size = 1.5, colour = "blue", alpha =
0.9) +
geom_line(aes(x= Segments, y=variable_count)
,stat="identity", group = 1, size = 1.5, colour = "darkgreen",
alpha = 0.9) +
geom_line(aes(x= Segments, y=children_count)
,stat="identity", group = 1, size = 1.5, colour = "orange", alpha
= 0.9) +
guides(fill=guide_legend(title="Segments")) +
scale_color_discrete(name = "Prod", labels = c("count", "bond_count", "variable_count", "children_count)))
I am fairly new to R so if any further information is required or if this question could be better represented then please let me know.
Any help is greatly appreciated.
Alright, you need to remove a little bit of your stuff. I used the mtcars dataset, since you did not provide yours. I tried to keep your variable names and reduced the plot to necessary parts. The code is as follows:
first_q <- mtcars
first_q$Segments <- mtcars$mpg
first_q$val <- seq(1,nrow(mtcars))
first_q$number_of_new_costumers <- mtcars$hp
first_q$type <- "Line"
ggplot(first_q) +
geom_bar(aes(x= Segments, y= number_of_new_costumers, fill = "Bar"), stat =
"identity") + theme(axis.text.x = element_blank()) +
scale_y_continuous(expand = c(0, 0), limits = c(0,3000)) +
geom_line(aes(x=Segments,y=val, linetype="Line"))+
geom_line(aes(x=Segments,y=disp, linetype="next line"))
The answer you linked already gave the answer, but i try to explain. You want to plot the legend by using different properties of your data. So if you want to use different lines, you can declare this in your aes. This is what get's shown in your legend. So i used two different geom_lines here. Since the aes is both linetype, both get shown at the legend linetype.
the plot:
You can adapt this easily to your use. Make sure you using known keywords for the aesthetic if you want to solve it this way. Also you can change the title names afterwards by using:
labs(fill = "costum name")
If you want to add colours and the same line types, you can do customizing by using scale_linetype_manual like follows (i did not use fill for the bars this time):
library(ggplot2)
first_q <- mtcars
first_q$Segments <- mtcars$mpg
first_q$val <- seq(1,nrow(mtcars))
first_q$number_of_new_costumers <- mtcars$hp
first_q$type <- "Line"
cols = c("red", "green")
ggplot(first_q) +
geom_bar(aes(x= Segments, y= number_of_new_costumers), stat =
"identity") + theme(axis.text.x = element_blank()) +
scale_y_continuous(expand = c(0, 0), limits = c(0,3000)) +
geom_line(aes(x=Segments,y=val, linetype="solid"), color = "red", alpha = 0.4)+
geom_line(aes(x=Segments,y=disp, linetype="second"), color ="green", alpha = 0.5)+
scale_linetype_manual(values = c("solid","solid"),
guide = guide_legend(override.aes = list(colour = cols)))

Remove lines from color and fill legends

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:

Changing line color when I have a geom_errorbar with ggplot

I have the following code:
library(ggplot2)
library(gridExtra)
data = data.frame(fit = c(9.8,15.4,17.6,21.6,10.8), lower = c(7.15,12.75,14.95,18.95,8.15), upper = c(12.44,18.04,20.24,24.24,13.44), factors = c(15,20,25,30,35), var = rep("Fator", 5))
gp <- ggplot(data, aes(x=factors, y=fit, ymax=upper, ymin=lower))
gp <- gp + geom_line(aes(group=var),size=1.2) +
geom_errorbar(width=.8, size=1, aes(colour='red')) +
geom_point(size=4, shape=21, fill="grey") +
labs(x = paste("\n",data$var[1],sep=""), y =paste("Values","\n",sep="")) +
theme(legend.position = 'none', axis.text = element_text(size = 11), plot.margin=unit(c(0.4,0.4,0.4,0.4), "cm"), axis.text.x = element_text(angle=45, hjust = 1, vjust = 1)) +
ylim((min(data$lower)), (max(data$upper)))
I want to change the line color after I have the ggplot object. I'm trying:
gp + scale_color_manual(values = "green")
but it change the error bar color and not the line color.
1)What should I do to change the line color?
2)How can I change the points color?
Thanks!
Try this:
gp$layers[[1]] <- NULL
gp + geom_line(aes(group = var),color = "green",size = 1.2)
A similar technique should work for the points layer. Technique was dredged up from my memories of a similar question.
I just looked at the contents of gp$layers manually to see which was which. I presume that the order will be the order in which they appear in your code, but I wouldn't necessarily rely on that.

ggplot2: How to have a different color of geom_vline()

I have this five (5) geom_vline() in my plot and I want them to have a different colors. Is there a way to do that?
Here's my codes,
library(ggplot2)
x <- seq(-7, 8, length = 90)
tvalues <- dt(x,15)
qplot(x, tvalues) + geom_polygon(fill = "purple", colour = "purple", alpha = 0.5) +
geom_point(fill = "purple", colour = "purple", alpha = 0.2, pch = 21) +
geom_vline(xintercept = c(a <- c(-2.27685371, 0.01661155,
0.33598194, 1.92426022), mean(a)), linetype = "dashed", colour = "red") + theme_bw() + xlab(bquote(bold('Average Tensile Strength (lb/in'^'2'*')'))) +
ylab(expression(bold(P(x)))) +
opts(title = expression(bold("Student t Distribution")), plot.title = theme_text(size = 20, colour = "darkblue"),
panel.border = theme_rect(size = 2, colour = "red"))
And here is the output,
Notice the five vertical lines in the plot, I want each of those line to have a different color,
I tried this
library(colorRamps)
geom_vline(xintercept = c(a <- c(-2.27685371, 0.01661155,
0.33598194, 1.92426022), mean(a)), linetype = "dashed", colour = matlab.like(5))
but didn't work, Another attempt
geom_vline(xintercept = c(a <- c(-2.27685371, 0.01661155,
0.33598194, 1.92426022), mean(a)), linetype = "dashed", colour = c("red","blue","green","yellow","orange"))
and still unsuccessful.
Thanks in advance!
So you're sort of missing the fundamental idea behind ggplot2, which is that you always put all your data into a data.frame and every aesthetic that you map corresponds to a variable in your data frame.
You could get 5 vertical lines, each of a different color with five separate calls to geom_vline but that misses the point of the entire package. Instead, you create a data frame:
a <- c(-2.27685371,0.01661155,0.33598194,1.92426022)
vlines <- data.frame(xint = c(a,mean(a)),grp = letters[1:5])
I've explicitly created a grouping variable grp to map to colour. Then we add the layer and map the aesthetics to these variables using aes:
qplot(x, tvalues) +
geom_polygon(fill = "purple", colour = "purple", alpha = 0.5) +
geom_point(fill = "purple", colour = "purple", alpha = 0.2, pch = 21) +
geom_vline(data = vlines,aes(xintercept = xint,colour = grp), linetype = "dashed") +
theme_bw() +
xlab(bquote(bold('Average Tensile Strength (lb/in'^'2'*')'))) +
ylab(expression(bold(P(x)))) +
opts(title = expression(bold("Student t Distribution")),
plot.title = theme_text(size = 20, colour = "darkblue"),
panel.border = theme_rect(size = 2, colour = "red"))
(The colors will be hard to distinguish because they're dashed lines, and two of them are nearly on top of each other.)
You will get much more out of ggplot2 if you transition away from qplot towards ggplot() and start putting your data into data frames rather than vectors.
If you just want to put one line, adding the following geom would add a vertical line at x=1 in red colour and dashed.
+ geom_vline(aes(xintercept=1), colour="#BB0000", linetype="dashed")

Resources