Related
The following code plots two partly overlapping density distributions from two independent dataframes with different lenghts.
library(ggplot2)
#Define colors to be used in plot for each group
mycolRO <- rgb(0.8, 0.2, 0, max = 1, alpha = 0.5) #Color for Group "Road"
mycolRA <- rgb(0.2, 0.6, 0.4, max = 1, alpha = 0.5) #Color for Group "Rail"
#Create some data
dfRoad <- data.frame(DiffRO=2+rnorm(300))
dfRail <- data.frame(DiffRA=rnorm(500))
#Plot density distributions
ggplot() +
geom_density(aes(x=DiffRO, fill = mycolRO, alpha=0.5), data=dfRoad) +
geom_density(aes(x=DiffRA, fill = mycolRA, alpha=0.5), data=dfRail) +
xlim(-6, 6) +
theme_classic() +
ggtitle("") +
xlab("Value") +
ylab("Density") +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text=element_text(size=15))+
labs(fill = "Group")+
theme(legend.title = element_text(color = "black", size = 15), legend.text = element_text(color = "black", size=12))+
theme(legend.position = c(0.2,0.8), legend.direction = "vertical")+
guides(alpha=FALSE)
The legend does show the correct base color, but not with the transparency (alpha) value defined above, which should be alpha=0.5.
Furthermore I would like to see the correct variable names ("DiffRO" and "DiffRA") as legend entries instead of the color codes.
Thanks for any help.
Here are two ways of doing what you want.
Common points to both are:
The colors are set manually with scale_fill_manual.
theme calls are simplified, there is no need to call theme repeatedly.
First, I will recreate the data, this time setting the RNG seed before calling rnorm.
set.seed(1234)
dfRoad <- data.frame(DiffRO = 2 + rnorm(300))
dfRail <- data.frame(DiffRA = rnorm(500))
Your way, corrected.
The legend labels must also be set manually in scale_fill_manual.
#Plot density distributions
ggplot() +
geom_density(aes(x=DiffRO, fill = mycolRO, alpha=0.5), data=dfRoad) +
geom_density(aes(x=DiffRA, fill = mycolRA, alpha=0.5), data=dfRail) +
xlim(-6, 6) +
ggtitle("") +
xlab("Value") +
ylab("Density") +
scale_fill_manual(labels = c("Road", "Rail"),
values = c(mycolRO, mycolRA)) +
theme_classic() +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text=element_text(size=15),
legend.title = element_text(color = "black", size = 15),
legend.text = element_text(color = "black", size=12),
legend.position = c(0.2,0.8), legend.direction = "vertical")+
labs(fill = "Group") +
guides(alpha = FALSE)
Another way, simpler.
The data is combined and reformated from two different data sets in one data set only. To do this I use package reshape2.
dflong <- reshape2::melt(dfRoad)
dflong <- rbind(dflong, reshape2::melt(dfRail))
Note that now only one call to geom_density is needed and that the legend labels are automatic.
ggplot(dflong, aes(x = value, group = variable, fill = variable, alpha = 0.5)) +
geom_density() +
xlim(-6, 6) +
ggtitle("") +
xlab("Value") +
ylab("Density") +
scale_fill_manual(values = c(mycolRA, mycolRO)) +
theme_classic() +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text = element_text(size=15),
legend.title = element_text(color = "black", size = 15),
legend.text = element_text(color = "black", size=12),
legend.position = c(0.2,0.8), legend.direction = "vertical") +
labs(fill = "Group") +
guides(alpha = FALSE)
I'm a beginner using R and I'm creating a ggplot-geom_bar (stack) to demonstrate diferences in percentage of compounds. However I have more than 11 colours and I cannot change the fill colours
I'm using the following code:
vermelha_bio_stack <- ggplot() +
geom_bar(data=vermelha_pom,
aes(y=values, x=month,fill= factor(compunds)),
stat="identity") +
xlab("") +
ylab("Percentages") +
scale_fill_discrete(name = "Compounds") +
scale_y_continuous(limits = c(0,101), expand=c(0,0)) +
theme_classic() +
theme(
text=element_text(family = "sans"),
axis.ticks.x = element_blank(),
axis.line.x.bottom = element_line (color = "black"),
axis.line.y.left = element_line(color = "black"),
panel.background = element_rect(fill = "white")) +
ggtitle("Lagoa Vermelha") +
theme(plot.title = element_text(color="black", size=14, face="bold", hjust = 0.5))+
coord_flip()
and I got the following plot
but I want colours like the following image:
I have a .csv file, test1.csv that looks like this:
seed,rate,TYPE,SUFFIX
1,1,A,Sim
1,1,A,Ana
2,1,A,Ana
2,2,A,Ana
1,1,B,Sim
1,1,B,Ana
2,1,B,Ana
2,2,B,Ana
1,1,C,Sim
2,2,C,Ana
I want to set alpha = 1 for rate = 1 and
alpha = 0 for rate = 2
I have the following R code:
require(ggplot2)
require(scales)
require(dplyr)
pdf(file="sweep_feasibility-vs-injection_test.pdf", height=3, width=6)
a<-read.csv('./test1.csv',header=T);
a$alphayr <- as.factor(ifelse(a$rate == 1, TRUE, FALSE))
a<-na.omit(a)
p<-ggplot(a,aes(x=rate,group=factor(SUFFIX))) +
geom_bar(stat="count", position = "dodge", aes(fill=factor(SUFFIX), alpha=alphayr))+
scale_alpha_manual(values = c(0,1), guide = F) +
facet_grid(~TYPE)+
xlab('Injection Rate (%)') +
ylab('Feasible (%)');
p + theme_bw() %+replace% theme(axis.title=element_text(),axis.title.y=theme_bw()$axis.title.y) +
theme(
axis.line=element_line(color='black'),
legend.position="top",
legend.background=element_rect(fill="transparent"),
axis.title.x = element_text(size=15),
axis.title.y = element_text(size=15),
panel.border=element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
legend.key.width=unit(0.5,"cm"),
legend.key.height=unit(0.5,"cm"),
plot.title = element_text(hjust = 0.5),
legend.key = element_rect(colour = "black", size=0.1),
legend.title=element_text(size=10),
axis.text.y=element_text(size=15,color='black'),
axis.text.x=element_text(angle=0,hjust=0.5, size=15, color='black'),
legend.text=element_text(size=15))+
guides(
fill=guide_legend(nrow=1,title=""),
color=guide_legend(nrow=1,title=""),
shape=guide_legend(nrow=1,title="")
);
when I run this code, I get the following graph:
I don't understand why I am seeing a bar for rate = 2
The cause of this is group = factor(SUFFIX) which, I think, isn't really a relevant aesthetic for geom_bar at all. Erasing that gives
I am trying to build a stack of ggplot2 density plots, like so:
And I have lined up / limited the x-axes so that then these grpahs are stacked using the gridExtra package, the ticks line up perfectly. However, in doing so, what I thought was a solid x-axis marker before turns out be a bottom "marker" line at the bottom of the density plot:
Is there anyway to add back in some sort of x-axis? The plots look somewhat naked/empty without it. I understand it more clearly indicates the limits of the data, but it looks unfinished and broken.
Edit
Here is the code I am using:
g <- ggplot(df_L, aes(x=values, linetype= type)) +
geom_density() +
ggtitle(expression('Low Region: '~LI[i]~'and'~WI[i])) +
scale_x_continuous(breaks = c(seq(0,100,10)), expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(xlim = c(0,100)) +
theme(text = element_text(size=20),
plot.title = element_text(size=14, vjust=1.5, hjust=0.5),
axis.title.x=element_blank(),
axis.title.y = element_blank(),
legend.position = c(0.1, 0.75),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=14,vjust=0,hjust=0),
legend.key.height = unit(1, 'line'),
legend.key.width = unit(1, 'line'),
panel.background = element_rect(fill = "white")) +
scale_linetype_manual(values=c(1,2,3),
labels=c(expression(LI[i]),expression(WI[i]))) +
guides(linetype = guide_legend(title=NULL))
g
I think the issue is that in the theme you're using (default) doesn't have a specified x-axis (or y-axis for that matter), but the axis-positions are implied by the grid. So you need to specifically add axis, either by using for instance +theme_bw(), or by adding something in the theme. I have done that (in red, you can really see it):
set.seed(124)
df_L <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:3],1000,T))
g <- ggplot(df_L, aes(x=values, linetype= type)) +
geom_density() +
ggtitle(expression('Low Region: '~LI[i]~'and'~WI[i])) +
scale_x_continuous(breaks = c(seq(0,100,10)), expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(xlim = c(0,100)) +
theme(text = element_text(size=20),
plot.title = element_text(size=14, vjust=1.5, hjust=0.5),
axis.title.x=element_blank(),
axis.title.y = element_blank(),
legend.position = c(0.1, 0.75),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=14,vjust=0,hjust=0),
legend.key.height = unit(1, 'line'),
legend.key.width = unit(1, 'line'),
panel.background = element_rect(fill = "white"),
axis.line=element_line(colour="red",size=2)) +
scale_linetype_manual(values=c(1,2,3),
labels=c(expression(LI[i]),expression(WI[i]))) +
guides(linetype = guide_legend(title=NULL))
I have the following plot:
ggplot(proba[108:140,], aes(c,four, color="a1")) +
geom_line(linetype=1, size=0.3) +
scale_x_continuous(breaks=seq(110,140,5)) +
theme_bw() +
theme(axis.line = element_line(colour = "black", size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) +
theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) +
theme(axis.ticks=element_line(colour="black",size=0.25)) +
xlab("\nTime-steps")+ylab("Proportion correct\n") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8)) +
geom_line(aes(c,three, color="a2"), size=0.2, linetype=2) +
geom_line(aes(c,one, color="a3"),linetype=3, size=0.8) +
geom_line(aes(c,two, color="a4"), linetype=1, size=0.6) +
scale_color_manual(values=c("a1"="red3", "a2"="red3","a3"="blue3","a4"="blue3")) +
theme(legend.title = element_text(colour="black", size=7)) +
theme(legend.position="bottom" ,
legend.direction="horizontal",
legend.title=theme_blank()) +
theme(legend.text=theme_text(size=7),
legend.background=theme_blank(),
legend.key=theme_blank())
The four lines on the plot are displayed in different linetypes, however the legend does not show these different linetypes only the different colours. I am obviously missing a very simple argument in theme() but I cannot figure out what it is. Maybe I need to specify the linetypes again in scale_colour_manual()?
Here's the data: https://dl.dropboxusercontent.com/u/22681355/proba.csv
proba<-read.csv("proba.csv",head=T)
Here, this is the correct way to do this in ggplot2:
proba <- read.csv("~/Downloads/proba.csv")
proba1 <- proba[108:140,]
proba1 <- melt(proba1,id.vars = 1:2)
ggplot(proba1,aes(x = c,y = value,colour = variable,linetype = variable,size = variable)) +
geom_line() +
scale_x_continuous(breaks=seq(110,140,5)) +
scale_colour_manual(values=c("blue3","blue3","red3","red3")) +
scale_linetype_manual(values = c(2,1,3,1)) +
scale_size_manual(values = c(0.2,0.3,0.8,0.6)) +
xlab("\nTime-steps") +
ylab("Proportion correct\n") +
theme_bw() +
theme(axis.text=element_text(size=6),
axis.title=element_text(size=8),
axis.line = element_line(size=0.25),
axis.ticks=element_line(size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="bottom" ,
legend.direction="horizontal",
legend.title=element_blank(),
legend.text=element_text(size=7),
legend.background=element_blank(),
legend.key=element_blank())
Try to keep all your theme adjustments in one call to theme, otherwise things get pretty messy. Also, you had some calls to theme_* that should have been element_*.