I have several geometric functions, specifically geom_point and geom_abline. I want some of them to have the same color by mapping the color aesthetic to custom values (that are not defined by a column in the dataframe).
In the example below, I want the lines to have to the same color as the surrounding points (without setting color='red' outside of aes() or adding a grouping column to the original data).
library(tidyverse)
ggplot(mtcars) +
geom_point(aes(x=disp, y=wt, color='wt')) +
geom_point(aes(x=disp, y=qsec, color='qsec')) +
geom_abline(intercept=0, slope=0.01, aes(color='wt')) +
geom_abline(intercept=20, slope=-0.01, aes(color='qsec'))
How do I do that?
As discussed here
The way to do this is
library(tidyverse)
ggplot(mtcars) +
geom_point(aes(x=disp, y=wt, color='wt')) +
geom_point(aes(x=disp, y=qsec, color='qsec')) +
geom_abline(aes(intercept=b, slope=m, color='wt'), data.frame(b=0, m=0.01)) +
geom_abline(aes(intercept=b, slope=m, color='qsec'), data.frame(b=20, m=-0.01))
Related
I am trying to color bars in ggplot but having issues. Can someone explain how to correctly use the fill parameter and the scale_colour parameters?
library(ggplot2)
df<-data.frame(c(80,33,30),c("Too militarized","Just doing their job","Unfairly tarnished by a few"),c("57%","23%","21%"))
colnames(df)<-c("values","names","percentages")
ggplot(df,aes(names,values))+
geom_bar(stat = "identity",position = "dodge",fill=names)+
geom_text(aes(label=percentages), vjust=0)+
ylab("percentage")+
xlab("thought")+
scale_colour_manual(values = rainbow(nrow(df)))
Working barplot example
barplot(c(df$values),names=c("Too militarized","Just doing their job","Unfairly tarnished by a few"),col = rainbow(nrow(df)))
The main issue is that you don't have fill inside a call to aes in geom_bar(). When mapping from data to visuals like colors, it has to be inside aes(). You can fix this by either wrapping fill=names with aes() or by just specifying fill colors directly, instead of using names:
Option 1 (no legend):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", fill=rainbow(nrow(df))) +
ylab("percentage") +
xlab("thought")
Option 2 (legend, because mapping from data to colors):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", aes(fill=names)) +
ylab("percentage") +
xlab("thought") +
scale_fill_manual(values=rainbow(nrow(df)))
Note that in both cases you might want to explicitly factor df$names ahead of the call to ggplot in order to get the bars in the order you want.
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 want to add a legend for filled rectangles in the background but I already used fill aesthetics for filling the bars of my bar plot.
How can I get the legend or create a matching legend by hand?
df <- data.frame(a=factor(c('a','b','c','c','d','e'), levels=c('a','b','c','d','e')),
x=seq(1,6),
b=factor(c('A','A','A','B','B','B'), levels=c('A','B')),
c=c(1,2,3,4,5,6),
d=rnorm(6))
ggplot(df, aes(x, c, fill=d, group=b)) +
geom_rect(aes(xmin=0.5,xmax=3.5,ymin=-Inf,ymax=Inf),alpha=0.05,fill="#E41A1C") +
geom_rect(aes(xmin=3.5,xmax=6.5,ymin=-Inf,ymax=Inf),alpha=0.05,fill="#377EB8") +
geom_bar(stat='identity', position=position_dodge()) +
coord_flip() +
scale_x_continuous(breaks=df$x, labels=df$a)
So I need a legend describing my two geom_rect areas. I was not able to map my two areas in any way to get a legend. In general the column df$b is describing the areas I do now by hand.
You can set colour= to variable b inside the aes() of both geom_rect(). This will make lines around the rectangles and also make legend. Lines can be removed setting size=0 for geom_rect(). Now using guides() and override.aes= you can change fill= for legend key.
ggplot(df, aes(x, c, fill=d, group=b)) +
geom_rect(aes(xmin=0.5,xmax=3.5,ymin=-Inf,ymax=Inf,colour=b),alpha=0.05,fill="#E41A1C",size=0) +
geom_rect(aes(xmin=3.5,xmax=6.5,ymin=-Inf,ymax=Inf,colour=b),alpha=0.05,fill="#377EB8",size=0) +
geom_bar(stat='identity', position=position_dodge()) +
coord_flip() +
scale_x_continuous(breaks=df$x, labels=df$a)+
guides(colour=guide_legend(override.aes=list(fill=c("#E41A1C","#377EB8"),alpha=0.3)))
I have a plot in which I am displaying individual values from multiple subjects, coloured by group. Added to that are means per group, calculated using stat_summary.
I would like the two means to be coloured by group, but in colours other than the individual data. This turns out to be difficult, at least when using stat_summary. I have the following code:
ggplot(data=dat,
aes(x=Round, y=DV, group=Subject, colour=T1)) +
geom_line() + geom_point() + theme_bw() +
stat_summary(fun.y=mean, geom="line", size=1.5,
linetype="dotted", color="black",
aes(group=T1))
Which produces this example graph.
The colour for the means created by stat_summary is set to black; otherwise it would be red and blue like the individual data lines. However, it is not possible to set more than one colour - so color=c("black", "blue") does not work.
I've already tried scale_colour_manual as explained here, but this will change the colours of the individual data lines, leaving the mean lines unaffected.
Any suggestion how to solve this? Code and data here.
You need to create different values for the mapping to color:
ggplot(data=iris,
aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_line() + geom_point() + theme_bw() +
stat_summary(fun.y=mean, geom="line", size=1.5,
linetype="dotted", aes(color=paste("mean", Species)))
You can then use scale_color_manual to get specific colors.
I am new to ggplot2. I would like to create a line plot that has points on them where the points are filled with different colors than the lines (see the plot below).
Suppose the dataset I am working with is the one below:
set.seed(100)
data<-data.frame(dv=c(rnorm(30), rnorm(30, mean=1), rnorm(30, mean=2)),
iv=rep(1:30, 3),
group=rep(letters[1:3], each=30))
I tried the following code:
p<-ggplot(data, aes(x=iv, y=dv, group=group, pch=group)) + geom_line() + geom_point()
p + scale_color_manual(values=rep("black",3))+ scale_shape(c(19,20,21)) +
scale_fill_manual(values=c("blue", "red","gray"))
p + scale_shape(c(19,20,21)) + scale_fill_manual(values=c("blue", "red","gray"))
But I do not get what I want.I hope someone can point me to the right direction. Thanks!
scale_fill_manual(), scale_shape_manual() and scale_colour_manual() can be used only if you have set fill=, shape= or colour= inside the aes().
To change colour just for the points you should add colour=group inside geom_point() call.
ggplot(data, aes(x=iv, y=dv, group=group,shape=group)) +
geom_line() + geom_point(aes(colour=group)) +
scale_shape_manual(values=c(19,20,21))+
scale_colour_manual(values=c("blue", "red","gray"))