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
Related
I want to add a latex expression in my legend like red-colored. I tried the following code, but I could not do that. How do I manage this issue? Also, I got a waring about this code. How do I remove that?
library(ggplot2)
ggplot(C, aes(sigma, value)) +
geom_line(aes(linetype=name)) +
scale_linetype_manual(values=c("solid", "twodash", "dotted")) +
scale_linetype_discrete(labels = c(expression("X_A = 1 \\& X_B =0"),
expression("X_A = 3 \\& X_B =0"),
expression("X_A = 5 \\& X_B =0"))) +
theme_minimal() +
xlab("standard diviation") +
ylab("rejection rate") +
theme_classic() +
theme(legend.position = c(0.15, 0.85),
legend.background = element_rect(fill="white",
size=0.5,
linetype="solid",
colour ="gray"),
legend.spacing.y = unit(0, "pt"),
text = element_text(size=20),
legend.text = element_text(size=20),
axis.title=element_text(size=20),
legend.title = element_blank(),
panel.grid.major.y = element_line(),
panel.grid.minor.y = element_line())
Scale for 'linetype' is already present. Adding another scale for 'linetype',
which will replace the existing scale.
I am trying to create a graph in R with ggplot. The graph is fine until I try to add labels with geom_text.
Data:
year <-c(2016,2017,2016,2017,2016,2017,2016,2017,2016,2017,2016,2017,2016,2017)
age <- c("0-15","0-15","16-25","16-25","26-35","26-35","36-45","36-45","46-55","46-55","56-65","56-65","66+","66+")
deaths <- c(10,4,40,33,38,28,23,22,18,22,13,16,44,33)
age_group <- factor(age)
fyear <- factor(year)
ideaths <- data.frame(fyear,age_group,deaths)
This is the code I have so far
ggplot(data = ideaths,mapping = aes(x = age_group, y=deaths,
fill=fyear)) +
geom_bar(position = "dodge", stat="identity", width=0.5) +
geom_text(label=deaths,vjust=-0.5) + ggtitle("Figure 8.") +
scale_fill_manual(values=c("#7F7F7F","#94D451")) +
scale_y_continuous(breaks=seq(0,55,5)) + theme_light() +
theme(panel.border = element_blank(), panel.grid.major.x =
element_blank(), panel.grid.minor.y =
element_blank(),panel.grid.major.y = element_line( size=.1,
color="grey"), axis.title = element_blank(), legend.position
= "bottom", legend.title=element_blank(), plot.title
=element_text(size=10))
Which gives me this graph:
I searched for how to align the labels with the bars and found position=position_dodge(width=0.9)
However, this puts the label over the wrong bar for me.
If anyone has any idea of how to fix this, or what is causing it in the first place it would be greatly appreciated!
You need to put label = deaths inside aes() so ggplot knows that it needs to use the deaths column inside ideaths data frame not the standalone deaths vector
library(ggplot2)
ggplot(data = ideaths, aes(x = age_group, y = deaths, fill = fyear)) +
geom_col(position = position_dodge(width = 0.9)) +
geom_text(aes(x = age_group, y = deaths + 3, label = deaths),
position = position_dodge(width = 0.9)) +
ggtitle("Figure 8.") +
scale_fill_manual(values = c("#7F7F7F", "#94D451")) +
scale_y_continuous(breaks = seq(0, 55, 5)) +
theme_light() +
theme(
panel.border = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(size = .1, color = "grey"),
axis.title = element_blank(), legend.position = "bottom",
legend.title = element_blank(), plot.title = element_text(size = 10)
)
Created on 2018-11-19 by the reprex package (v0.2.1.9000)
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"))
I'd like to make a forest plot for my project. Since it is not a typical forest plot built-in any R package, I found the first figure of this page is helpful to my goal, a side table accompanied with the forest plot:
https://mcfromnz.wordpress.com/2012/11/06/forest-plots-in-r-ggplot-with-side-table/
The code which produces that particular figure is pasted below (the original link:https://github.com/nzcoops/blog_code/blob/master/forest_plot.Rmd)
The problem that I ran into is in the "data_table" step. An error pop up when I type the following in R:
data_table
Error: Aesthetics must be either length 1 or the same as the data (28): yintercept
I guess the issue came from geom_hlinein data_table.
After some online search and some try-and-error, I still cannot get rid of that error message and wonder if I can get some help here. Thanks in advance for your help.
--Code that particular produce the first figure:
library(ggplot2)
library(gridExtra)
dat <- data.frame(group = factor(c("A","B","C","D","E","F","G"), levels=c("F","E","D","C","B","A","G")),
cen = c(3.1,2.0,1.6,3.2,3.6,7.6,NA),
low = c(2,0.9,0.8,1.5,2,4.2,NA),
high = c(6,4,2,6,5,14.5,NA))
theme_set(theme_bw())
theme_update(
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(0,0,0,0), "lines"))
p <- ggplot(dat,aes(cen,group)) +
geom_point(size=5, shape=18) +
geom_errorbarh(aes(xmax = high, xmin = low), height = 0.15) +
geom_vline(xintercept = 1, linetype = "longdash") +
scale_x_continuous(breaks = seq(0,14,1), labels = seq(0,14,1)) +
labs(x="Adjusted Odds Ratio", y="")
data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5,7.5))) +
theme(panel.grid.major = element_blank(),
legend.position = "none",
panel.border = element_blank(),
axis.text.x = element_text(colour="white"),#element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_line(colour="white"),#element_blank(),
plot.margin = unit(c(0,0,0,0), "lines")) +
labs(x="",y="") +
coord_cartesian(xlim=c(1,4.5))
lab <- data.frame(V0 = factor(c("A","B","C","D","E","F","G","A","B","C","D","E","F","G","A","B","C","D","E","F","G","A","B","C","D","E","F","G"),, levels=c("G","F","E","D","C","B","A")),
V05 = rep(c(1,2,3,4),each=7),
V1 = c("Occuption","Active","","Inactive","","Inactive","","Recreation","Inactive","","Active","","Inactive","","Gender","Men","Women","Men","Women","Men","Women","OR",3.1,2.0,1.6,3.2,3.6,7.6))
data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5,7.5))) +
theme(panel.grid.major = element_blank(),
legend.position = "none",
panel.border = element_blank(),
axis.text.x = element_text(colour="white"),#element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_line(colour="white"),#element_blank(),
plot.margin = unit(c(0,0,0,0), "lines")) +
labs(x="",y="") +
coord_cartesian(xlim=c(1,4.5))
The easiest fix would be separating geom_hline into 2 different calls
data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5))) +
geom_hline(aes(yintercept=c(7.5))) +
theme(panel.grid.major = element_blank(),
legend.position = "none",
panel.border = element_blank(),
axis.text.x = element_text(colour="white"),#element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_line(colour="white"),#element_blank(),
plot.margin = unit(c(0,0,0,0), "lines")) +
labs(x="",y="") +
coord_cartesian(xlim=c(1,4.5))
data_table
Created on 2018-03-31 by the reprex package (v0.2.0).
You don't need to use aes() with geom_hline (only use aes() if you want a horizontal line for every row of your data.) You can just do:
geom_hline(yintercept = c(6.5, 7.5))
This is explained in the help, see ?geom_hline for more details.
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).