R ggplot : Can't change y-axis scale range with facetted plot - r

I have a simple problem but I can't figure out why it won't work -> I can't adjust the y scale range on my faceted bar plot:
# Data #
df<-as.data.frame(c("x","y","z","x","y","z","x","y","z","x","y","z"))
colnames(df)<-"x"
df$y<-c(10,15,20,5,25,45,10,10,20,40,20,5)
df$facet<-c(1,1,1,1,1,1,2,2,2,2,2,2)
df$group<-c("A","A","A","B","B","B","A","A","A","B","B","B")
# Plot #
ggplot(df, aes(x=x, y=y, fill=group)) +
facet_grid( ~ facet) +
scale_fill_manual(values=c("blue", "red")) +
geom_bar(position="dodge", stat="identity") +
theme(strip.text = element_text(face="bold", size=rel(1)),
strip.background = element_rect(fill="white", colour="white", size=1)) +
theme(panel.margin = unit(1, "lines")) +
scale_x_discrete(expand = c(0, 0)) +
theme(panel.grid.major.x = element_blank()) + theme(axis.ticks.x = element_blank()) +
theme(legend.background=element_blank()) +
scale_y_continuous("%", breaks=seq(0, 50, 10), minor_breaks=seq(0,50,5), expand = c(0, 0))
I would like the y-axis to go upto 50 but using scale_y_continuous strangely does not work, producing this result:

You need to add a limits argument in your scale_y_continuous :
scale_y_continuous("%", limits=c(0,50), breaks=seq(0, 50, 10), minor_breaks=seq(0,50,5), expand = c(0, 0))
Otherwise you just define the breaks position, not the axis values range.

Related

Scale the distance between ticks on x-axis in gglot r

I have a graph like this. I am interested in the minor change between the range (-3.5, 0.5), however, the occupied only a small portion of the x-axis, so that it's hard to interpret.
I tried to use transform to log scale for better visualization, however, it apparently not works for negative values.
So is there any method to expand this region to make the graph look nicer?
Code:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18))
Something like this
library(ggforce)
library(ggolot2)
ggplot(mtcars, aes(x=mpg, y=disp, group=1)) +
geom_line() + facet_zoom(xlim = c(15, 20))
You may try adding the xlim = c(minor value, major value) option of ggplot, and use the range which works better for you
Something like that:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18)) +
xlim = c(-4, 1)

Broken confidence interval areas when using ylim in ggplot2

I've been using ggplot2 for long, but never experienced this issue. I am representing confidence intervals of some regressions. However, I decided to manually control the ylim(). I realized that those areas which exceed the y limits are broken. See this picture:
The red regression on the right contains a very wide CLs. As you can see there is a gap in there as its highest point is outside ylim range.
This is the code I used:
ggplot(dataset, aes(x=variable, y=value, fill=Species, colour=Species, linetype = Species)) +
geom_smooth(method="lm", formula= y~poly(x,3), level=0.95, alpha=0.2) +
xlab("A") +
ylab("B") +
ylim(0, 30) +
theme(axis.text.x = element_text(angle = 0, hjust = 0.5, size = 10),
panel.background = element_blank(),
legend.position='bottom',
panel.grid.major = element_line(colour="azure2"),
axis.line = element_line(colour = "black",
size = 0.15, linetype = "solid")) +
scale_x_continuous(breaks=seq(1, 10, 1), limits=c(1, 10)) +
scale_color_manual(values=c("coral4", "coral1", "darkolivegreen3", "darkgoldenrod4", "darkgoldenrod2", "deepskyblue3", "darkorchid3")) +
scale_fill_manual(values=c("coral4", "coral1", "darkolivegreen3", "darkgoldenrod4", "darkgoldenrod2", "deepskyblue3", "darkorchid3")) +
scale_linetype_manual(values=c(1,1,1,3,3,2,2))
I would like to keep these y limits. I used coord_cartesian with no success. Can anybody help me?
coord_cartesian should work, but you have to remove the ylim()
Some data
set.seed(1)
df <- data_frame(x = -5:5, y = rnorm(11, x^2, 5))
Replicating your problem
ggplot(df, aes(x, y)) +
geom_smooth() +
ylim(-1, NA)
With coord_cartesian
ggplot(df, aes(x, y)) +
geom_smooth() +
coord_cartesian(ylim = c(-1, 40))

Align ggplot bar labels to axis of plot

I would like the barplot labels to start at 0 on the y axis. Right now, I can adjust them wrt the end of the bar, but that's not what I want.
data.df <- data.frame(Proportions=c(.4,.2,.25,.4,.12,.16),Name=c("Label one","Label two","Label three","Label one","Label two","Label three"),group=c("group.one","group.one","group.one","group.two","group.two","group.two"))
ex.plot <-
ggplot(data.df, aes(x=Name,y=Proportions,fill=group))+
geom_bar(stat='identity')+
xlab(NULL)+
ylab(NULL)+
scale_y_continuous(limits=c(0,1))+
scale_fill_manual(values=c("#EDA45E","#DB7365"))+
guides(fill=FALSE)+
geom_text(aes(label=Name),angle=90,hjust=1)+
theme_bw()+
theme(strip.text.x = element_blank())+
theme(panel.grid.major.x = element_blank(),axis.text.x=element_blank(),
axis.title.y = element_blank())+
facet_wrap(~group,nrow=1)
plot(ex.plot)
Not what I want:
geom_text(aes(label=Name, y=0), angle=90, hjust=0)
hjust=0 means left-justified.
Map the y aesthetic to 0.
...
geom_text(aes(label = Name, y = 0), angle = 90, hjust = 0) +
...

How to make data points equal to 0 appear on axis using ggplot? [duplicate]

This question already has answers here:
Avoid clipping of points along axis in ggplot
(4 answers)
Closed 6 years ago.
I am trying to make my data points that are equal to 0 appear on the x axis and avoid having data points cut by the axis.
I also want to keep my origin as it is (same 0 for both axis).
Here is my graph:
I used this to obtain the origin at 0 and to set the limits of my axis:
scale_y_continuous(limits = c(0, 40), expand = c(0, 0))+
scale_x_continuous(limits = c(0, 30), expand = c(0, 0))
If I change the limit of y for
scale_y_continuous(limits = c(-1, 40))
it also changes the origin...
Here is all I my code in case you need it:
require(ggplot2)
myplot<-ggplot(data=NULL,aes(x = Length, y = Complete)) +
geom_point(data = C,shape=1, size=3)+
geom_point(data = T, size=3)+
scale_y_continuous(limits = c(0, 40), expand = c(0, 0))+
scale_x_continuous(limits = c(0, 30), expand = c(0, 0))+
xlab("Inflorescence Length (cm)")+
ylab("Successful Weevils")+
theme_bw() +
theme(panel.border = element_blank())+
theme(panel.grid.major = element_blank())+
theme(panel.grid.minor = element_blank())+
theme(axis.text=element_text(size=12))+
theme(axis.title=element_text(size=14,face="bold"))+
theme(axis.line.x=element_line())+
theme(axis.line.y=element_line())+
theme(plot.margin = unit(c(1,1,1,1), "cm"))+
stat_smooth(data = C, method = lm, se=FALSE, color="grey")+
stat_smooth(data = T, method = lm, se=FALSE, color="black")
myplot
Thank you!
You have to turn off clipping:
library(grid)
library(ggplot2)
df <- data.frame(x=1:1000, y=runif(1000))
p <- ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(limits = c(0,1), expand=c(0,0))
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

Overlay density of one variable on trend line

I have some data:
dat <- data.frame(x=rnorm(100,100,100),y=rnorm(100,100,100))
I can plot it with a local trend line:
ggplot(dat, aes(x,y)) + stat_smooth()
But I want to overlay a density curve, on the same plot, showing the distribution of x. So just add the previous graph to this one (the y-axis is different, but I only care about relative differences in the density curve anyway):
ggplot(dat, aes(x)) + geom_density()
I know there's stat_binhex() and stat_sum() etc showing where the data falls. There are only a few y values, so what gets plotted by stat_binhex() etc is hard to read.
You can plot a combination of histograms and density curves at both sides of the scatterplot. In the example below I also included a confidence ellipse:
require(ggplot2)
require(gridExtra)
require(devtools)
source_url("https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R") # in order to create a 95% confidence ellipse
htop <- ggplot(data=dat, aes(x=x)) +
geom_histogram(aes(y=..density..), fill = "white", color = "black", binwidth = 2) +
stat_density(colour = "blue", geom="line", size = 1.5, position="identity", show_guide=FALSE) +
scale_x_continuous("x-var", limits = c(-200,400), breaks = c(-200,0,200,400)) +
scale_y_continuous("Density", breaks=c(0.0,0.01,0.02), labels=c(0.0,0.01,0.02)) +
theme_bw() + theme(axis.title.x = element_blank())
blank <- ggplot() + geom_point(aes(1,1), colour="white") +
theme(axis.ticks=element_blank(), panel.background=element_blank(), panel.grid=element_blank(),
axis.text.x=element_blank(), axis.text.y=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank())
scatter <- ggplot(data=dat, aes(x=x, y=y)) +
geom_point(size = 0.6) + stat_ellipse(level = 0.95, size = 1, color="green") +
scale_x_continuous("x-var", limits = c(-200,400), breaks = c(-200,0,200,400)) +
scale_y_continuous("y-var", limits = c(-200,400), breaks = c(-200,0,200,400)) +
theme_bw()
hright <- ggplot(data=dat, aes(x=y)) +
geom_histogram(aes(y=..density..), fill = "white", color = "black", binwidth = 1) +
stat_density(colour = "red", geom="line", size = 1, position="identity", show_guide=FALSE) +
scale_x_continuous("y-var", limits = c(-200,400), breaks = c(-200,0,200,400)) +
scale_y_continuous("Density", breaks=c(0.0,0.01,0.02), labels=c(0.0,0.01,0.02)) +
coord_flip() + theme_bw() + theme(axis.title.y = element_blank())
grid.arrange(htop, blank, scatter, hright, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))
the result:

Resources