customising my Facet grid with error bars - r

Hi everyone I want to do a grid bar plot of my data which is the relative abundance of fungi and make it as neat as possible but when I use facet grid the bar plots don't look right to facilitate comparison. Here is the data:
When I use the code :
theme_set(theme_bw())
facet_plot <- ggplot(my_data, aes(x = depth, y = relative abundance, fill = Treatment)) +
geom_bar(stat = "identity", position = "dodge") +
geom_errorbar(aes(ymin=relative abundance-se, ymax=relative abundance+se), width=.2,position=position_dodge(.9))+
facet_grid(. ~ phylum)
I get a plot that looks like this:
As you can see , the plot looks strange especially the last three barplots. Does anyone know how I can modify my code so each plot has its own y axis or any other way of adjusting the scale?
Best wishes

Related

How to draw bar plot including different groups in R with ggplot2?

I want to draw a combined bar plot, so that I can make comparision among different score types.
compare_data = data.frame(model=c(lr,rf,gbm,xgboost),
precision=c(0.6593,0.7588,0.6510,0.7344),
recall=c(0.5808,0.6306,0.4897,0.6416),f1=c(0.6176,0.6888,0.5589,0.6848),
acuracy=c(0.6766,0.7393,0.6453,0.7328))
compare1 <- ggplot(evaluation_4model, aes(x=Model, y=Precision)) +
geom_bar(aes(fill = Model), stat="identity")
compare1 <- compare+labs(title = "Precision")
Here is one of the barplot I draw, and this is the type of "precision", however, I want to make a wide bar plot, with all the models under 4 score types sharing the same Y-axis, also with subtitle if possible.
Your code throws an error, because evaluation_4model is not defined.
However, the answer to your problem is likely to make a faceted plot and hence melt the data to a long format. To do this, I usually make use of the reshape library. Tweaking your code looks like this
library(ggplot2)
library(reshape2)
compare_data = data.frame(model=c("lr","rf","gbm","xgboost"),
precision=c(0.6593,0.7588,0.6510,0.7344),
recall=c(0.5808,0.6306,0.4897,0.6416),
f1=c(0.6176,0.6888,0.5589,0.6848),
acuracy=c(0.6766,0.7393,0.6453,0.7328))
plotdata <- melt(compare_data,id.vars = "model")
compare2 <- ggplot(plotdata, aes(x=model, y=value)) +
geom_bar(aes(fill = model), stat="identity")+
facet_grid(~variable)
compare2
does that help?

Visualizing just top portion of stacked barplot in ggplot2 in R

I made a stacked barplot in ggplot2 in R:
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill")+ scale_y_continuous(name = "Y-axis",labels = scales::percent)
I want to just visualize the top portion of the stacked barplot like so:
I've looked everywhere and can't figure out how to do this. Does anyone know how?
You can use coord_cartesian to "zoom in" on the area you want.
# your plot code...
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill") +
scale_y_continuous(name = "Y-axis",labels = scales::percent) +
# set axis limits in coord_cartesian
coord_cartesian(ylim = c(0.75, 1))
Note that many people consider bar plots that don't start at 0 misleading. A line plot may be a better way to visualize this data.
Since the areas you want to show are less than 20% of the total area, you could flip the bar charts so that you only show the colors areas. Then the y-axis goes from 0-25% and you can use the figure caption to describe that the remaining data is in the gray category.

How to add percentage labels within a ggplot bar chart when using factors to fill

Edit: This question is not a duplicate of How to draw stacked bars in ggplot2 that show percentages based on group? . The data there is not purely factors and includes a "y =" in
ggplot(df, aes(x = factor(year), y = amount, fill = type))
As far as I know, "y =" is not relevant to my example.
That said, my goal is to display the percentages for the factor used in fill within ggplot. This is a bit different than other questions because I don't want to display the percentage for the x-variable—I want the percentages to be based on the fill variable, which will be a factor. It's probably easiest to illustrate with mtcars.
df<-data(mtcars)
ggplot(mtcars, aes(x = factor(mtcars$vs),fill=factor(mtcars$am))) +
geom_bar(aes(y=(..count..)/sum(..count..))) +
scale_y_continuous(labels=percent)
This gives us:
What I want, however, is

Using Geom_Bar in GGplot2

So I'm trying to use geom_bar in ggplot2, and all of the cases that I see of people demonstrating it online are of comparative frequencies of certain things. The chart that I'm trying to do is the stacked bar graph like this one
However, I want to do it from a vector of values. That is, let's say I have the vector
v=c(1,2,3,4)
Instead of 4 even bars, which is what I understand I would get, I'd like a stack of 4 bars where the top one is 1 unit tall, and the next one down is 2 units tall (etc.). Is this possible in R?
Edit: Here is the code that I've used for my graph. It's yielding a normal bar graph, not the stacked version that I'm looking for:
ggplot(data = v, aes(x = factor(x), y = y)) + geom_bar(aes(fill = factor(y)),stat = 'identity')
I think you can start from this:
v=data.frame(x="My Stacked Bar", y=c(1,2,3,4))
ggplot(data = v, aes(x = factor(x), y = y))+
geom_bar(aes(fill=factor(y)), stat="identity")

hHw to make width of bars equal in ggplot2 barplot?

I am trying to make the width of all bars in the following plot equal. Can anybody help me? is it possible? or is there any way to plot this data?
library(ggplot2)
dat <- data.frame(x = c('I','I','I','I','II','II'),
y = LETTERS[1:6],
z = abs(rnorm(6)))
ggplot(dat, aes(y,z))+
geom_bar(stat = "identity") +
facet_wrap(~x,scales="free")
I also tried using arguments size and width inside the geom_bar but its not working.
Really the problem is that each of the facet panels is being forced to be the same size and then the plot inside expands to fill all the available room. With facet_grid you can adjust the space for each facet panel (but you cannot seem to do this with facet_wrap). Try
ggplot(dat, aes(y,z))+
geom_bar(stat = "identity") +
facet_grid(~x,scales="free", space="free_x")
which gives me

Resources