I want to plot a path and show where the datapoints are.
Combine Points with lines with ggplot2
uses geom_point() + geom_line() but I do not like that the dots are much thicker and the lines have a discontinuous look - x - x ----- x --- thus I decidet to
create my own dotted line:
mya <- data.frame(a=1:20)
ggplot() +
geom_path(data=mya, aes(x=a, y=a, colour=2, size=1)) +
geom_point(data=mya, aes(x=a, y=a, colour=1, size=1)) +
theme_bw() +
theme(text=element_text(size=11))
I like that the dots and the line have the same size. I did not use the alpha channel because I fear trouble with the alpha channel when I include the files in other programs.
open problems:
R should not create those legends
can R calculate the "darker colour" itself? darker(FF0000) = AA0000
how can I manipulate the linethickness? The size= parameter did not work as expected in R 2.15
Aesthetics can be set or mapped within a ggplot call.
An aesthetic defined within aes(...) is mapped from the data, and a legend created.
An aesthetic may also be set to a single value, by defining it outside aes().
In your case it appears you want to set the size to a single value. You can also use scale_..._manual(values = ..., guide = 'none') to suppress the creation of a legend.
This appears to be what you want with colour.
You can then use named colours such as lightblue and darkblue (see ?colors for more details)
ggplot() +
geom_line(data=mya, aes(x=a, y=a, colour='light'), size = 2) +
geom_point(data=mya, aes(x=a, y=a, colour='dark'), size = 2) +
scale_colour_manual(values = setNames(c('darkblue','lightblue'),
c('dark','light')), guide = 'none') +
theme_bw()
Related
I have a data frame like `dat1:
dat1 <- data.frame(idx = 1:200,
fit = rnorm(200,10,0.5))
cis <- data.frame(uci=dat1$fit+0.5,
lci = dat1$fit-0.5)
dat1 <- cbind(dat1,cis)
I also have 3 other objects where I have stored "points of interest" (or poi1:3) for dat1.
poi1 <- c(30,59,120,150)
poi2 <- c(10,42,110,165,190)
poi3 <- c(50, 100)
I made a line plot with confidence bands for dat1 using this code:
p<-
ggplot(dat1, aes(x=idx, y=fit))+
geom_line()+
geom_ribbon(aes(ymin = lci, ymax = uci), alpha = 0.3)+
labs(x="Distance", y="Var1")
p
I want to highlight the "points of interest" along this line. I want to highlight the points for poi1 red, the points for poi2 blue, and the points for poi3 green. I can use geom_vline() to make them all vertical:
p+
geom_vline(xintercept =poi1, color="red")+
geom_vline(xintercept = poi2, color = "blue")+
geom_vline(xintercept = poi3, color = "green")
But I would actually like the points for poi1 and poi2 to be either blue and red points (instead of lines, and leaving poi3 as a vertical green line), or much "shorter" versions of what is done by geom_vline (both above and below the black line). I cannot get geom_point() to behave correctly and do this. Do I need to format it differently, or how can I accomplish this?
Also, how can I next add a legend in the top corner that denotes which line/point and color denotes which group of poi? For instance if they are points (plus the green line) it will have a red point and say "poi1" a blue point next to the word "poi2" and a small green line next to the work "poi3"
Probably the most straightforward way to add the colored points is to have separate calls to geom_point and supply poi1, poi2, and poi3 as the method to subset your plot.
So, the code would look something like this:
p +
geom_point(data=dat1[which(dat1$idx %in% poi1),], color='red', size=3) +
geom_point(data=dat1[which(dat1$idx %in% poi2),], color='blue', size=3) +
geom_point(data=dat1[which(dat1$idx %in% poi3),], color='green', size=3)
The one problem here is that there's no reference to what those colors are supposed to be (as in... a legend). If you want to add that, you would put color= within aes(...) so that a legend key is created, and assign the name of the key item within (e.g. aes(color="name of item 1")). Then you would need a scale_color_manual(...) call to set the colors specifically... or you can do without the scale_color_manual object and just leave it up to ggplot to figure out the color scheme.
Assigning the colors manually in legend:
p +
geom_point(data=dat1[which(dat1$idx %in% poi1),], aes(color='poi1'), size=3) +
geom_point(data=dat1[which(dat1$idx %in% poi2),], aes(color='poi2'), size=3) +
geom_point(data=dat1[which(dat1$idx %in% poi3),], aes(color='poi3'), size=3) +
scale_color_manual(values=list('poi1'='red','poi2'='blue','poi3'='green'))
Without manual color assignment. ggplot just uses the theme's default color palette.
p +
geom_point(data=dat1[which(dat1$idx %in% poi1),], aes(color='poi1'), size=3) +
geom_point(data=dat1[which(dat1$idx %in% poi2),], aes(color='poi2'), size=3) +
geom_point(data=dat1[which(dat1$idx %in% poi3),], aes(color='poi3'), size=3)
Adding a vertical line and showing that in the legend
Related to the follow up question in the comment to this solution: How would you add a vertical line for xintercept=poi3 and have that shown in the legend?
You could just add individual calls to geom_vline with separate values for xintercept=, but if you have many this is just kind of bad practice. A better practice is to have one geom_vline call where you set the data= as poi3. ggplot wants to receive a dataframe as data=, so in the code below you'll see I force this conversion via as.data.frame(.... Forcing a data frame makes a datframe of 2 observations with one column called "poi3", so that's what we will assign as the xintercept= aesthetic. This works... but you'll see it's not quite right:
p +
geom_point(data=dat1[which(dat1$idx %in% poi1),], aes(color='poi1'), size=3) +
geom_point(data=dat1[which(dat1$idx %in% poi2),], aes(color='poi2'), size=3) +
geom_vline(data=as.data.frame(poi3), aes(xintercept=poi3, color='poi3'),
linetype=2, size=1) +
scale_color_manual(values=list('poi1'='red','poi2'='blue','poi3'='green2'))
The green line is added to the legend, but it's added "behind" all the points as a line + point. What we want is a separate legend. ggplot2 tries to combine legends where possible. What we'll do is kind of a cheat, which is to force ggplot2 to create another legend for linetype, and just set color='green2' outside of aes() in the geom_vline call. This removes the color of the line from the legend created for "colour" and adds another legend for "linetype" showing only our line. The final fix is to set the value for linetype via scale_manual_linetype. Since there's only one key here, you only need one value.
p +
geom_point(data=dat1[which(dat1$idx %in% poi1),], aes(color='poi1'), size=3) +
geom_point(data=dat1[which(dat1$idx %in% poi2),], aes(color='poi2'), size=3) +
geom_vline(
data=as.data.frame(poi3), aes(xintercept=poi3, linetype='poi3'),
color='green2', size=1) +
scale_color_manual(values=list('poi1'='red','poi2'='blue')) +
scale_linetype_manual(values=2)
I'm creating a plot with ggplot that uses colored points, vertical lines, and horizontal lines to display the data. Ideally, I'd like to use two different color or linetype scales for the geom_vline and geom_hline layers, but ggplot discourages/disallows multiple variables mapped to the same aesthetic.
# Create example data
library(tidyverse)
library(lubridate)
set.seed(1234)
example.df <- data_frame(dt = seq(ymd("2016-01-01"), ymd("2016-12-31"), by="1 day"),
value = rnorm(366),
grp = sample(LETTERS[1:3], 366, replace=TRUE))
date.lines <- data_frame(dt = ymd(c("2016-04-01", "2016-10-31")),
dt.label = c("April Fools'", "Halloween"))
value.lines <- data_frame(value = c(-1, 1),
value.label = c("Threshold 1", "Threshold 2"))
If I set linetype aesthetics for both geom_*lines, they get put in the
linetype legend together, which doesn't necessarily make logical sense
ggplot(example.df, aes(x=dt, y=value, colour=grp)) +
geom_hline(data=value.lines, aes(yintercept=value, linetype=value.label)) +
geom_vline(data=date.lines, aes(xintercept=as.numeric(dt), linetype=dt.label)) +
geom_point(size=1) +
scale_x_date() +
theme_minimal()
Alternatively, I could set one of the lines to use a colour aesthetic,
but then that again puts the legend lines in an illogical legend
grouping
ggplot(example.df, aes(x=dt, y=value, colour=grp)) +
geom_hline(data=value.lines, aes(yintercept=value, colour=value.label)) +
geom_vline(data=date.lines, aes(xintercept=as.numeric(dt), linetype=dt.label)) +
geom_point(size=1) +
scale_x_date() +
theme_minimal()
The only partial solution I've found is to use a fill aesthetic instead
of colour in geom_pointand setting shape=21 to use a fillable shape,
but that forces a black border around the points. I can get rid of the
border by manually setting color="white, but then the white border
covers up points. If I set colour=NA, no points are plotted.
ggplot(example.df, aes(x=dt, y=value, fill=grp)) +
geom_hline(data=value.lines, aes(yintercept=value, colour=value.label)) +
geom_vline(data=date.lines, aes(xintercept=as.numeric(dt), linetype=dt.label)) +
geom_point(shape=21, size=2, colour="white") +
scale_x_date() +
theme_minimal()
This might be a case where ggplot's "you can't have two variables mapped
to the same aesthetic" rule can/should be broken, but I can't figure out clean way around it. Using fill with geom_point shows the most promise, but there's no way to remove the point borders.
Any ideas for plotting two different color or linetype aesthetics here?
I have couple of questions regarding plotting using ggplot2.
I have already used below commands to colour data points using R.
library(ggplot2)
df <- read.csv(file="c:\\query2.csv")
ggplot( df,aes( x = Time,y ,y = users,colour = users>40) ) + geom_point()
My question is: how should I draw a continuous line connecting data points and how do I circle around data points for users >40?
To connect the points, use geom_line (if that doesn't give you what you need, please explain what you're trying to accomplish).
I haven't used geom_encircle, but another option is to use a filled marker with the fill deleted to create the circles. Here's an example, using the built-in mtcars data frame for illustration:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_point(data=mtcars[mtcars$mpg>30,],
pch=21, fill=NA, size=4, colour="red", stroke=1) +
theme_bw()
pch=21 is one of the filled markers (see ?pch for more info on other available point markers). We set fill=NA to remove the fill. stroke sets the thickness of the circle border.
UPDATE: To add a line to this chart, using the example above:
ggplot(mtcars, aes(wt, mpg)) +
geom_line() +
geom_point() +
geom_point(data=mtcars[mtcars$mpg>30,],
pch=21, fill=NA, size=4, colour="red", stroke=1) +
theme_bw()
However, if (as in my original code for this graph) you put the aes statement inside the geom, rather than in the initial call to ggplot, then you need to include an aes statement inside geom_line as well.
I am trying to change the style settings of this kind of chart and hope you can help me.
R code:
set_theme(theme_bw)
cglac$pred2<-as.factor(cglac$pred)
ggplot(cglac, aes(x=depth, colour=pred2))
+ geom_bar(aes(y=..density..),binwidth=3, alpha=.5, position="stack")
+ geom_density(alpha=.2)
+ xlab("Depth (m)")
+ ylab("Counts & Density")
+ coord_flip()
+ scale_x_reverse()
+ theme_bw()
which produces this graph:
Here some points:
What I want is to have the density line as black and white lines separated by symbols rather than colour (dashed line, dotted line etc).
The other thing is the histogram itself. How do I get rid of the grey background in the bars?
Can I change the bars also to black and white symbol lines (shaded etc)? So that they would match the density lines?
Last but not least I want to add a second x or in this case y axis, because of flip_coord(). The one I see right now is for the density. The other one I need would then be the count data from the pred2 variable.
Thanks for helping.
Best,
Moritz
Have different line types: inside aes(), put linetype = pred2. To make the line color black, inside geom_density, add an argument color = "black".
The "background" of the bars is called "fill". Inside geom_bar, you can set fill = NA for no fill. A more common approach is to fill in the bars with the colors, inside aes() specify fill = pred2. You might consider faceting by your variable, + facet_wrap(~ pred2, nrow = 1) might look very nice.
Shaded bars in ggplot? No, you can't do that easily. See the answers to this question for other options and hacks.
Second y-axis, similar to the shaded symbol lines, the ggplot creator thinks a second y-axis is a terrible design choice, so you can't do it at all easily. Here's a related question, including Hadley's point of view:
I believe plots with separate y scales (not y-scales that are transformations of each other) are fundamentally flawed.
It's definitely worth considering his point of view, and asking yourself if those design choices are really what you want.
Different linetypes for densities
Here's my built-in data version of what you're trying to do:
ggplot(mtcars, aes(x = hp,
linetype = cyl,
group = cyl,
color = cyl)) +
geom_histogram(aes(y=..density.., fill = cyl),
alpha=.5, position="stack") +
geom_density(color = "black") +
coord_flip() +
theme_bw()
And what I think you should do instead. This version uses facets instead of stacking/colors/linetypes. You seem to be aiming for black and white, which isn't a problem at all in this version.
ggplot(mtcars, aes(x = hp,
group = cyl)) +
geom_histogram(aes(y=..density..),
alpha=.5) +
geom_density() +
facet_wrap(~ cyl, nrow = 1) +
coord_flip() +
theme_bw()
I constructed the following plot using ggplot using the following code:
ggplot(data, aes(x=Variable, y=Value, fill=Yield.Type)) +
geom_bar(stat="identity", position="dodge")
I had two questions:
1) How do I change the colour of the bar: I want to colour the pink bar as white and blue bar as grey with black borders. If in the code, I use col="White",fill="White", it colours both of them with the same colour and also stacks them up on each other
2) For each bar, I have the standard error in separate vector
For pink bars, se1<-c(0.08,0.07,0.08,0.07)
For blue bars, se2<-c(0.07,0.1,0.06,0.06)
I wanted to know how to add this standard errors to resepctive batch
How do I add this to the bar?
Please provide data it is much easier to answer. Here I have created a new set.
I have included martin's answer for the color
If you have a se value per bar, your should directly add it inside your data frame and use
geom_errorbar(). Check documentation for more info.
.
Variable <- factor(c("VAr1","VAr1","Var2","Var2","Var3","Var3","VAr4","VAr4"))
Yield.Type <- factor(c('O','R','O','R','O','R','O','R'))
Value <- c(1,2,3,4,3,5,6,5)
se1<-c(0.08,0.07,0.08,0.07,0.1,0.06,0.1,1)
data <- data.frame(Variable,Yield.Type,Value,se1)
limits <- aes(ymax = Value + se1, ymin=Value - se1)
dodge <- position_dodge(width=0.8)
ggplot(data, aes(x=Variable, y=Value,fill=Yield.Type,colour=Yield.Type)) +
geom_bar(stat="identity", position="dodge")+
scale_color_manual(values=c("black","black")) +
scale_fill_manual(values=c("white", "grey"))+
geom_errorbar(limits, position=dodge,width=0.1)
First question: use scale_color_manual and scale_fill_manual (see add different colour to bar plot)
p <- ggplot(...)
p + scale_color_manual(values=c("white","black")) +
scale_fill_manual(values=c("white", "grey"))
p
Second qestion: Look here or R: ggplot2 barplot and error bar for help.