Increase size in scale colour manual ggplot2 - R [duplicate] - r

I am trying to override my symbol size aes using commands I have found in this forum and elsewhere. I can get the alpha override to work but not the size.
f1<-ggplot(data=d, aes(x=rpos, y=count, group=id,color=id)) +
geom_point(alpha=0.05, size=0.5) +
scale_fill_manual(values=c("blue", "red")) +
scale_colour_manual(values=c("blue", "red")) +
xlab("Chromosome 1") +
scale_y_continuous(name="Relative coverage",limits=c(-0.5,1.5)) +
guides(colour =guide_legend(override.aes=list(size=5))) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
theme_bw()
optns <- theme (
plot.title = element_text(face="bold", size=14),
axis.title.x = element_text(size=12),
axis.title.y = element_text(size=12, angle=90),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(.97,.85),
legend.text = element_text(size=10),
legend.key.size = unit(1, "lines"),
legend.key = element_blank(),
legend.justification = 'right'
)
f1 + ggtitle ("Coverage of Chromosome 1") +
optns + ggsave("Rel.mergedDataChr1.pdf", width = 20, height = 5, dpi = 100)

Your alpha override is overwriting your size override. Change this:
guides(colour = guide_legend(override.aes = list(size=5))) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
to this:
guides(colour = guide_legend(override.aes = list(size=5, alpha = 1))) +
In the future, please try to include minimal working examples. This means both providing data (to make it a working example) and also not bothering with things like all your optns (to keep it minimal).

Related

ggplot2 density plot legend shows wrong group names and wrong colors

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)

Problem with alpha transparency in ggplot2 in R

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

Specific Barplot in R ggplot

Hello I will try for a last time,
I am doing my best to draw a barplot like the following Figure:
However it seems impossible with R.
Any idea?
Thanks in advance,
Peter
Attached the code I used.
groupe2<-rep(c(rep("P",4),rep("I",4)),2)
groupe<-rep(c("PPP","PPI","PIP","PII","IPP","IPI","IIP","III"),2)
OR_A<-c(1.00,0.86,0.88,0.90,0.77,0.68,0.77,0.70)
ICinf_A<-c(NA,0.70,0.72,0.76,0.60,0.50,0.61,0.61)
ICsup_A<-c(NA,1.06,1.07,1.06,1.00,0.92,0.96,0.81)
OR_B<-c(1.00,0.97,1.01,0.81,0.73,0.69,0.61,0.58)
ICinf_B<-c(NA,0.78,0.77,0.62,0.61,0.57,0.50,0.52)
ICsup_B<-c(NA,1.20,1.28,1.05,0.81,0.82,0.71,0.65)
OR_C<-c(1.00,1.03,0.86,0.65,0.68,0.58,0.47,0.37)
ICinf_C<-c(NA,0.84,0.67,0.50,0.59,0.49,0.40,0.33)
ICsup_C<-c(NA,1.27,1.10,0.86,0.78,0.69,0.56,0.41)
Cohort<-c(rep(" PC",8), rep("RIC",8))#, rep("RIC",8))
OR<-c(OR_A,OR_B)#,OR_C)
ICinf<-c(ICinf_A,ICinf_B)#,ICinf_C)
ICsup<-c(ICsup_A,ICsup_B)#,ICsup_C)
rm(dataOR)
dataOR<-data.frame(OR,groupe,Cohort,groupe2,ICinf,ICsup)
names(dataOR)
dataOR[, "groupe"] <- factor(dataOR[, "groupe"] ,
levels = c("PPP","PPI","PIP","PII","IPP","IPI","IIP","III"))
##########
library(ggdag)
ggplot(dataOR, aes(fill=outcome, y=OR, x=groupe)) +
geom_bar(position="dodge", stat="identity", color = "gray95", size = 0.25) +
# scale_fill_brewer(palette="Blues")+
scale_fill_manual(values = RColorBrewer::brewer.pal(5, "Blues")[3:5]) +
geom_errorbar(aes(ymin=ICinf, ymax=ICsup), width=.4, position=position_dodge(.9))+
geom_hline(yintercept=1) +
geom_point(position = position_dodge(0.9), size = 0.5, show.legend = F) +
scale_y_continuous(expand = expand_scale(mult = c(0, 0.05))) +
facet_wrap(~groupe, nrow = 1, scales = "free_x") +
labs(fill = NULL) +
theme(legend.position = "top",
legend.key.height = unit(0.2, "cm"),
legend.background = element_rect(color = "black", size = 0.4),
axis.line = element_line(color = "black"),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.grid.major.x = element_blank(),
axis.title = element_text(face = "bold"))

Using R to create an image don't want the white border or the title to appear only the PNG image

I'm using the code below to try to create a PNG image that is only the image but there is still some text and a white border.
theplot <- data %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
theme_void() + theme(legend.position="none") + theme(axis.title = element_blank())
I've also tried the following.
theplot <- data %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
theme_void() + theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())
I'm not very familiar with R so I'm not sure if ggplot is what I should be using to create just an image.
Is it ok like this ?
library(ggplot2)
library(ggthemes)
ggplot(mtcars, aes(mpg, wt)) + geom_point() +
theme(plot.margin=unit(c(0,0,0,0), unit="mm"))
+ theme_fivethirtyeight()
ggsave("myplot.png")
One can take a look at the code of theme_fivethirtyeight:
> theme_fivethirtyeight
function (base_size = 12, base_family = "sans")
{
(theme_foundation(base_size = base_size, base_family = base_family) +
theme(line = element_line(colour = "black"), rect = element_rect(fill = ggthemes_data$fivethirtyeight["ltgray"],
linetype = 0, colour = NA), text = element_text(colour = ggthemes_data$fivethirtyeight["dkgray"]),
axis.title = element_blank(), axis.text = element_text(),
axis.ticks = element_blank(), axis.line = element_blank(),
legend.background = element_rect(), legend.position = "bottom",
legend.direction = "horizontal", legend.box = "vertical",
panel.grid = element_line(colour = NULL), panel.grid.major = element_line(colour = ggthemes_data$fivethirtyeight["medgray"]),
panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0,
size = rel(1.5), face = "bold"), plot.margin = unit(c(1,
1, 1, 1), "lines"), strip.background = element_rect()))
}
I must say I don't understand why that doesn't work with your custom theme. I don't see a major difference.

Extend ggplot x-axis lines past range of data

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))

Resources