Space specific ares in x axis ggplot - r

I have the below script:
testFigure <- ggplot(data = final_df, aes(x=final_df$`ng DNA`,
y=final_df$`count`)) +
geom_point(col = "darkmagenta") + ggtitle("ng VS Number") +
xlab(expression(paste("ng"))) + ylab("Num (#)") +
theme(plot.title = element_text(hjust = 0.5, color="orange", size=18,
face="bold.italic"),
axis.title.x = element_text(color="#993333", size=10, face = "bold"),
axis.title.y = element_text(color="#993333", size=10,face = "bold")) +
scale_y_log10(breaks=c(0,10,50,200,600))
testFigure+scale_x_continuous(breaks=c(5,50,100,150,200,250,300,350,400)
Which generates the plot:
I'd like to space the area of the plot in the X axis, so that the lower values 0-10, but especially 0-5, will be more clear and spaced between them, while keeping the spaces of the other ticks.
Any suggestions how to do that?

Solution 1:
I also noticed you have done it but in wrong axis
scale_y_log10(breaks=c(0,10,50,200,600))
but it seems that you have to do the same thing for x axe as well
scale_x_log10(breaks=c(0,10,50,200,600))
Solution 2:
scale_x_discrete(limits=0:5)
You can use scale on each axis and set a limit over it
library(ggplot2)
dt<-data.frame("Name"=sample(c("A","B"),10,replace = T),
x=sample(1:10,10),y=sample(1:10,10))
ggplot(dt, aes( x= x , y= y))+
geom_point(stat='identity', aes(shape=Name,colour = Name))+
scale_x_discrete(limits=1:12)+
scale_y_discrete(limits=1:12)

Related

Putting horizontal lines on grouped boxplots

I am trying to make a boxplot with this basic code:
design=c("Red","Green","Blue")
actions=c("1","2","3","4","5","6","7","8")
proportion=(seq(1:240)+sample(1:500, 240, replace=T))/2000
df=data.frame(design, actions , proportion)
ggplot(df, aes(x=actions, y=proportion, fill=design)) +
geom_boxplot()+
xlab(TeX("group"))+
ylab("Y value")+
ggtitle("Y values for each group stratified by color")
Producing something like this:
I want to add horizontal lines for "true" Y values that are different for each group.
Does anyone have any tips for doing this? I don't know how to extract the width of each group of boxes, otherwise I could use geom_segment.
Here is a MWE with a non-grouped boxplot:
dBox <- data.frame(y = rnorm(10),group="1")
dBox=rbind(dBox,data.frame(y=rnorm(10),group="2"))
dLines <- data.frame(X =c(-0.36, 0.015),
Y = c(0.4, -0.2),
Xend = c(0.-0.015, 0.36),
Yend=c(0.4, -0.2),
group = c("True", "True"),
color = c("black", "red"))
ggplot(dBox, aes(x=0, y=y,fill=group)) +
geom_boxplot(outlier.shape = 1)+
geom_segment(data = dLines, aes(x = X, xend = Xend, y = Y, yend = Yend),color="red",size=1.5,linetype=1) +
theme(legend.background = element_rect(fill = "white", size = 0.1, linetype = "solid", colour = "black"))
This produces something like this:
However, it's difficult to make the geom_segments line up with the boxes exactly, and to then extend this to the grouped boxplot setting.
Thanks!
This can be done using a workaround with facets:
lines = data.frame(actions = 1:8, proportion=abs(rnorm(8)))
design=c("Red","Green","Blue")
actions=c("1","2","3","4","5","6","7","8")
proportion=(seq(1:240)+sample(1:500, 240, replace=T))/2000
df=data.frame(design, actions , proportion)
lines = data.frame(actions = 1:8, proportion=abs(rnorm(8)))
p = ggplot(df, aes(x=actions, y=proportion, fill=design)) +
geom_boxplot()+
xlab("group")+
ylab("Y value")+
ggtitle("Y values for each group stratified by color") +
facet_grid(~actions, scale='free_x') +
theme(
panel.spacing.x = unit(0, "lines"),
strip.background = element_blank(),
strip.text.x = element_blank())
p + geom_hline(aes(yintercept = proportion), lines)
You could probably fiddle around with removing the spaces between the facets to make it look more like what you intended.
Thanks to #eugene100hickey for pointing out how to remove spacing between facets.
theme(panel.spacing.x) can remove those pesky lines:
p + geom_hline(aes(yintercept = proportion), lines) +
theme(panel.spacing.x = unit(0, "lines"))

Cannot rotate x axis labels in ggplot2 facet graphs in r

Can anyone help out with a task?
I've been trying to run a part of code that creates a facet_wrap() graph in r, and it worked very well for my porpouse but the x axis labels are overlapping each other.
I need to rotate them 45 degrees.
I've been trying to run this code for the porpouse and it did create the graph, but the x axis label didn't rotate as I expected.
Here is the code:
for (x in unique(clss_perc_factor7$Horto)){
ggsave(paste('barplot_esp40', x, sep = '_'),
ggplot(clss_perc_factor7[clss_perc_factor7$Horto == x,],
aes(x = Classe,
y = Freq_perc)) +
theme(plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, hjust = 1)) +
theme_bw() +
geom_bar(stat = 'identity',
colour = 'black',
fill = '#66CC99') +
facet_wrap(~ CD_TALHAO) +
labs(x = 'Classe de Espaçamento',
y = 'Nº de Indivíduos (%)',
title = 'Distribuição do Espaçamento em Classes'),
dpi = 'retina',
device = 'png')
}
Given that:
clss_perc_factor7 is the data frame with more then one forest garden ("Horto" - in the loop).
In each forest garden there are different productive units ("CD_TALHAO" - in facet_wrap())
That's why I'm using a for loop to plot the graphs.
OBS: The part theme(plot.title = element_text(hjust = 0.5)... which was supposed to align the title to center isn't working either.
Thanks a lot guys!
Here is a way:
library(ggplot2)
ggplot(mtcars,aes(hp,mpg)) +
geom_point() +
facet_wrap(~cyl) +
theme(strip.text.x = element_text(angle = 45))
Good luck!

how to change x-axis labels in ggboxplot

I have a data frame including multiple factors. I used ggboxplot to get a box plot with comparisons for different categories. I am not satisfied with the x axis labels. I tried different ways but failed to get what I expected.
The code used to create a plot is:
df <- data.frame(country=sample(LETTERS[1:4], 1000, TRUE),
rating=round(rnorm(1000,70,15),1),
sex =rep(c("Female","Male"),500),
school=sample(c("public","private"),1000,TRUE))
df$group <- paste(df$school,df$sex,sep=".")
df <- df[order(df$group),]
my_comparisons <- list(c("public.Female","public.Male") , c("private.Female","private.Male"))
library(ggpubr)
ggboxplot(df, x = "group",y = "rating",
color = "group", palette = "simpsons",
add = "jitter",facet.by="country",legend="none", ylab="Rating") +
theme(strip.text.x=element_text(size=10, color="red", face="bold.italic"),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.x = element_blank()) +
stat_compare_means(method = "t.test",comparisons = my_comparisons,
label.y = 110,label = "p.signif")
The expected plot looks like:
This gets you close to what you're looking for (I couldn't figure out the line separator). You may also have to play around with the positioning of the labels to get them just right, as well as sizes.
ggboxplot(df, x = "group",y = "rating",
color = "group", palette = "simpsons",
add = "jitter", facet.by="country", legend="none", ylab="Rating") +
scale_x_discrete(labels=rep(c("F","M"),4)) +
theme(strip.text.x=element_text(size=10, color="red", face="bold.italic"),
axis.title.x = element_blank(),
plot.margin=unit(c(2,2,15,2), "mm")) +
stat_compare_means(method = "t.test",comparisons = my_comparisons,
label.y = 110, label = "p.signif") +
coord_cartesian(ylim=c(20,120), xlim=c(1,4), clip="off") +
annotate("text", x=1.5, y=0, label=c("","","Private","Private")) +
annotate("text", x=3.5, y=0, label=c("","","Public","Public")) +
annotate("text", x=0.5, y=10, label=c("","","Sex",""), hjust=1) +
annotate("text", x=0.5, y=0, label=c("","","School",""), hjust=1)
Additions include scale_x_discrete() to change x-axis labels, plot.margin and coord_cartesian to allow annotations outside the plot area, and annotate for each annotation, where the labels for each facet panel are given as a vector, with blanks for panels which shouldn't get labels.
There may be a cleaner way to do this, but the faceted nature of the plot means that annotations get replicated across facets which you don't want in this case.

Position dodge does not work with geom_point and geom_errorbar

I have this overplotting issue going on. Even after reading a lot of posts on dodge, jitter and jitter dodge in all kinds of implementations I can't figure it out.
Here you can get my data: http://pastebin.com/embed_js.php?i=uPXN7nPt
library(dplyr)
library(gdata)
library(ggplot2)
library(directlabels)
all<-read.xls('all_auto_bio_adjusted_c.xls')
all$size.new<-sqrt(all$size.new)
all$station<-as.factor(all$station)
all$group.new<-factor(all$group, levels=c('C. hyperboreus','C. glacialis','Special Calanus','M. longa','Pseudocalanus sp.','Copepoda'))
pd <- position_dodge(w = 50)
allp <- ggplot(data = all, aes(y = averagebiol, x = automatic, colour = group.new, group=group.new)) +
geom_abline(intercept = 0, slope = 1) +
geom_point(aes(size = size.new), show_guide=TRUE, position=pd) +
scale_size_identity()+
geom_errorbar(aes(ymin = averagebiol - stdevbiol, ymax = averagebiol + stdevbiol),colour = "grey", width = 0.1, position=pd) +
facet_grid(group.new~station, scales="free") +
xlab("Automatic identification") + ylab("Manual identification") +
ggtitle("Comparison of automatic vs manual identification") +
theme_bw() +
theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=15,angle=0,hjust=1,vjust=0,face="bold"), axis.title.x = element_text(colour="grey20",size=20,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=20,angle=90,hjust=.5,vjust=1,face="bold"), legend.position="none", strip.text.x = element_text(size = 12, face="bold", colour = "black", angle = 0), strip.text.y = element_text(size = 12, face="bold", colour = "black"))
allp
Which produces this nice plot
But as you can see a lot of the points and error bars are cramped together. Shouldn't my implementation of position dodge work?
If I understood right position dodge takes the scale of the axes, so with a doge of 50 I should see some results. I also tried putting the dodge argument directly into the geom, but that had no effect either.
Any ideas?
If you leave out position = pd in both geom_errorbar() and geom_point() you get the same plot. The reason the data look 'cramped' is because of the spread of the x-values. As far as I know, dodging will only happen if two points 'overlap', which I interpret as having the same x-value, e.g. data on a categorical x-axis like in the case of a bar plot. Your x-axis is continuous so the points will not be dodged.
To deal with the overplotting you could try logarithmic scales:
library(ggplot2)
tmp <- tempfile()
download.file("http://pastebin.com/raw.php?i=uPXN7nPt", tmp)
all <- read.csv(tmp)
all$size.new <- sqrt(all$size.new)
all$station <- as.factor(all$station)
all$group.new <- factor(all$group, levels = c("C. hyperboreus", "C. glacialis",
"Special Calanus", "M. longa",
"Pseudocalanus sp.", "Copepoda"))
# explicitly remove missing data
all <- all[complete.cases(all), ]
allp <- ggplot(data = all, aes(y = averagebiol, x = automatic, colour = group.new,
group = group.new, ymin = averagebiol - stdevbiol,
ymax = averagebiol + stdevbiol)) +
theme_bw() +
geom_abline(intercept = 0, slope = 1) +
geom_errorbar(colour = "grey", width = 0.1) +
geom_point(aes(size = size.new)) +
scale_size_area() + # Just so I could see all the points on my monitor :)
xlab("Automatic identification") +
ylab("Manual identification") +
ggtitle("Comparison of automatic vs manual identification")
allp + scale_x_log10() +
scale_y_log10() +
facet_grid(group.new ~ station, scales = "fixed")

Changing line color when I have a geom_errorbar with ggplot

I have the following code:
library(ggplot2)
library(gridExtra)
data = data.frame(fit = c(9.8,15.4,17.6,21.6,10.8), lower = c(7.15,12.75,14.95,18.95,8.15), upper = c(12.44,18.04,20.24,24.24,13.44), factors = c(15,20,25,30,35), var = rep("Fator", 5))
gp <- ggplot(data, aes(x=factors, y=fit, ymax=upper, ymin=lower))
gp <- gp + geom_line(aes(group=var),size=1.2) +
geom_errorbar(width=.8, size=1, aes(colour='red')) +
geom_point(size=4, shape=21, fill="grey") +
labs(x = paste("\n",data$var[1],sep=""), y =paste("Values","\n",sep="")) +
theme(legend.position = 'none', axis.text = element_text(size = 11), plot.margin=unit(c(0.4,0.4,0.4,0.4), "cm"), axis.text.x = element_text(angle=45, hjust = 1, vjust = 1)) +
ylim((min(data$lower)), (max(data$upper)))
I want to change the line color after I have the ggplot object. I'm trying:
gp + scale_color_manual(values = "green")
but it change the error bar color and not the line color.
1)What should I do to change the line color?
2)How can I change the points color?
Thanks!
Try this:
gp$layers[[1]] <- NULL
gp + geom_line(aes(group = var),color = "green",size = 1.2)
A similar technique should work for the points layer. Technique was dredged up from my memories of a similar question.
I just looked at the contents of gp$layers manually to see which was which. I presume that the order will be the order in which they appear in your code, but I wouldn't necessarily rely on that.

Resources