I have made a bar graph and would like it to have a log-scaled y axis. However, when I try to add the code for this, it for some reason inverses most of the bars... Can anyone explain why this may be and how to remedy this?
The code I use is as follows:
Graph1 <- ggplot(Data, aes(x=Temp, y=Mean, fill=Exposure)) +
geom_bar(position=position_dodge(), stat='identity', color="black",) +
xlab("Temperature (°C)") +
ylab("Cd concentration (µg/g)") +
facet_wrap(.~Day, scales="free_y", labeller=labeller(Day=supp.labsDAY), nrow = 1, ncol = 4)+
geom_errorbar( aes(x=Temp, ymin=Mean-SEM, ymax=Mean+SEM), width = 0.2, position=position_dodge(.9))
Graph1+ scale_y_log10()
Related
I had to flip the axis of my line, but still need the geom_area to be under the curve. However I cannot figure out how to do so.
This is the line of code I tried
ggplot(PalmBeachWell, aes(x=Date, y=Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_area(position= "identity", fill='lightblue') +
theme_classic() +
geom_line(color="blue") +
scale_y_reverse()
and here is what i got
One option would be to use a geom_ribbon to fill the area above the curve which after applying scale_y_reverse will result in a fill under the curve.
Using some fake example data based on the ggplot2::economics dataset:
library(ggplot2)
PalmBeachWell <- economics[c("date", "psavert")]
names(PalmBeachWell) <- c("Date", "Depth.to.Water.Below.Land.Surface.in.ft.")
ggplot(PalmBeachWell, aes(x = Date, y = Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_ribbon(aes(ymin = Depth.to.Water.Below.Land.Surface.in.ft., ymax = Inf),
fill = "lightblue"
) +
geom_line(color = "blue") +
scale_y_reverse() +
theme_classic()
I want my bar plot to take up less space, currently there is a lot of white space which is not neccessary. I would like the bins to be a lot closer than they currently are. And since there are only two categories in the X axis I do not see why there is so much space between them. - increasing the bin width would make white space go away, but then the bins become unnaturally large.
Code:
# Creates plot to show response-rate
hubspot_ordering %>%
ggplot(aes(x = Replied)) +
geom_bar(color = "black",
fill = "steelblue",
stat = "count",
width = 0.1,
position = position_dodge(0.9)) +
xlab("Kommuners respons på e-post og oppringing") +
scale_x_discrete(labels = c("Besvart",
"Ingen respons")) +
ylab("Antall kommuner") +
theme(element_line(size = 0.5)) +
theme_bw() -> plot_response_rate
Output:
You can change the aspect.ratio in your theme using the following code:
df <- data.frame(x=factor(LETTERS[1:2]), y=sample(1:100, 2))
library(ggplot2)
ggplot(data=df, aes(x=x, y=y, width=.1)) +
geom_bar(stat="identity", position="identity") +
theme(aspect.ratio = 2/1) +
labs(x="", y="")
Output aspect.ratio=2:
Output aspect.ratio=1:
this is my first post here, I'm having issues with getting the labels on the x axis of my second plot to show up when using ggarange, but they show up fine when I run them separately in R. Would anyone be able to give me some advice as to how I can fix this? Thanks in advance.
This is my code:
Plot 1
species_bias<- ggplot(Obs_Bias,aes(x=Species,y=Observations, fill=Species))+
geom_boxplot() +
stat_summary(fun = mean)+
theme_classic()+
ggtitle("Species Bias")+
theme(legend.position = "none")+
ylab("Relative Abundance")
species_bias
Plot 2
Abundace_PLOT<- ggplot(Abundance_Errorbar, aes(x=Species, y=Mean,fill=Method)) +
geom_bar(stat="identity", position=position_dodge()) +
geom_errorbar(aes(ymin=Mean-SE, ymax=Mean+SE),width=.2, position=position_dodge(0.9)) +
labs(x="Species", y="Relative Abundnace") +
theme_classic() +
ggtitle("Relative Abundance in Plastifauna grouped by Survey Method")
Abundace_PLOT
Combined plots on page
Species_Bias_ggarange<-ggarrange(species_bias,Abundace_PLOT + rremove("x.text"), labels = c("A","B"), ncol = 2, nrow = 1)
annotate_figure(Species_Bias_ggarange, top=text_grob("Visualizing Species Bias", face="bold",size=14))
Species_Bias_ggarange
I have a grid of plots, all with the same y and x-axis scale. The plots represent time in the x-axe and mean values in the y-axe with their standard errors. My problem is that some errorbars are not entirely within the plot margins, and I wonder if there is some way to represent the part of the errorlines that are within the plot margins. Below I give a fake example and code to play with:
df <- data.frame(time=seq(-15,15,1),
mean=c(0.49,0.5,0.53,0.55,0.57,0.59,0.61,0.63,0.65,0.67,0.69,0.71,0.73,0.75,0.77,0.79,0.77,0.75,0.73,0.71,0.69,0.67,0.65,0.63,0.61,0.59,0.57,0.55,0.53,0.51,0.49),
sd=c(0.09,0.087,0.082,0.08,0.023,0.011,0.010,0.009,0.008,0.007,0.006,0.005,0.004,0.003,0.002,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.010,0.011,0.023,0.08,0.084,0.087,0.09))
Plot <- ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(limits = c(0.49, 0.85), breaks = c(0.5, 0.65,0.8))
Plot
You need to set coord_cartesian limits rather than scale_y_continuous limits:
ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(breaks = c(0.5, 0.65,0.8)) +
coord_cartesian(ylim = c(0.49, 0.85))
When I run this code:
ggplot() +
stat_density2d(data = Unit_J, aes(x=X, y=Y, fill=..level.., alpha=0.9), lwd= 0.05, bins=50, col="blue", geom="polygon") +
scale_fill_continuous(low="blue",high="darkblue") +
scale_alpha(range=c(0, 0.03), guide="none") +
xlim(-6600,-3800) + ylim(400,2500) +
coord_fixed(expand=FALSE) +
geom_point(data = Unit_J, aes(x=X, y=Y), alpha=0.5, cex=0.4, col="darkblue") +
theme_bw() +
theme(legend.position="none")
I get this plot:
I know that increasing in this case X lims would solve the problem of unclosed lines shown on the left and right.
However, I want to keep these limits unchanged so that those "bugs" don't appear, and simply they must be beyond the limits, somehow hidden without creating those horrible lines.
Is there any possibility?
EDIT (download data here):
In order to ease and reproduce the example, you can download the data here
The trick is to expand the canvas using xlim and ylim so that there is enough room for ggplot to draw complete contours around the data. Then you can use tighter xlim and ylim parameters within the coord_fixed term to show the window you want...
ggplot() +
stat_density2d(data = Unit_J, aes(x=X, y=Y, fill=..level.., alpha=0.9),
lwd= 0.05, bins=50, col="blue", geom="polygon") +
scale_fill_continuous(low="blue",high="darkblue") +
scale_alpha(range=c(0, 0.03), guide="none") +
xlim(-7000,-3500) + ylim(400,2500) + #expanded in x direction
coord_fixed(expand=FALSE,xlim=c(-6600,-3800),ylim=c(400,2500)) + #added parameters
geom_point(data = Unit_J, aes(x=X, y=Y), alpha=0.5, cex=0.4, col="darkblue") +
theme_bw() +
theme(legend.position="none")