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?
Related
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
I have the following data frame, nothing to fancy about it.
df_bar<-data.frame(capacity = c(no[2],no[1]-no[2],max(df_l$load)-no[1]), type = c("Nuclear","Coal","Gas"),a = c("Optimal","Optimal","Optimal"))
I tried to create a stacked barplot via ggplot, but I also need to make sure that I have a specific order in that plot, with N being the closest to the axis and C the furthest. However, the simple code leads to this.
ggplot(df_bar, aes(y=capacity, x=a, fill=type)) +
geom_bar(position="stack", stat="identity")
How can I maybe alter the code so it respects the order I need?
1.Create a minimal reproducible example.
df_bar<-data.frame(capacity = c(2,2,2),
type = c("Nuclear","Coal","Gas"),
a = c("Optimal","Optimal","Optimal"))
ggplot2 respects the order of ordered factors when plotting. We can use that to our advantage:
library(ggplot2)
ggplot(df_bar, aes(y=capacity, x=a, fill=factor(type, levels=c( "Coal", "Gas", "Nuclear")))) +
geom_bar(position="stack", stat="identity") +
labs(fill="type")
I'm currently working on a very simple data.frame, containing three columns:
x contains x-coordinates of a set of points,
y contains y-coordinates of the set of points, and
weight contains a value associated to each point;
Now, working in ggplot2 I seem to be able to plot contour levels for these data, but i can't manage to find a way to fill the plot according to the variable weight. Here's the code that I used:
ggplot(df, aes(x,y, fill=weight)) +
geom_density_2d() +
coord_fixed(ratio = 1)
You can see that there's no filling whatsoever, sadly.
I've been trying for three days now, and I'm starting to get depressed.
Specifying fill=weight and/or color = weight in the general ggplot call, resulted in nothing. I've tried to use different geoms (tile, raster, polygon...), still nothing. Tried to specify the aes directly into the geom layer, also didn't work.
Tried to convert the object as a ppp but ggplot can't handle them, and also using base-R plotting didn't work. I have honestly no idea of what's wrong!
I'm attaching the first 10 points' data, which is spaced on an irregular grid:
x = c(-0.13397460,-0.31698730,-0.13397460,0.13397460,-0.28867513,-0.13397460,-0.31698730,-0.13397460,-0.28867513,-0.26794919)
y = c(-0.5000000,-0.6830127,-0.5000000,-0.2320508,-0.6547005,-0.5000000,-0.6830127,-0.5000000,-0.6547005,0.0000000)
weight = c(4.799250e-01,5.500250e-01,4.799250e-01,-2.130287e+12,5.798250e-01,4.799250e-01,5.500250e-01,4.799250e-01,5.798250e-01,6.618956e-01)
any advise? The desired output would be something along these lines:
click
Thank you in advance.
From your description geom_density doesn't sound right.
You could try geom_raster:
ggplot(df, aes(x,y, fill = weight)) +
geom_raster() +
coord_fixed(ratio = 1) +
scale_fill_gradientn(colours = rev(rainbow(7)) # colourmap
Here is a second-best using fill=..level... There is a good explanation on ..level.. here.
# load libraries
library(ggplot2)
library(RColorBrewer)
library(ggthemes)
# build your data.frame
df <- data.frame(x=x, y=y, weight=weight)
# build color Palette
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")
# Plot
ggplot(df, aes(x,y, fill=..level..) ) +
stat_density_2d( bins=11, geom = "polygon") +
scale_fill_gradientn(colours = myPalette(11)) +
theme_minimal() +
coord_fixed(ratio = 1)
Have an assignment where we need to provide one-dimensional graphs for EDA but the sample code given answers most of the requirements already (simple scatter and box plots and a histogram) so I am trying to "spice it up" a little by creating some more interesting graphs. Only need a couple.
The data set is the twin IQ data across several studies/authors and I was wanting to do a back-to-back histogram of the twins separated by author. So far I can do an overlay of authors or the back to back of the twins using ggplot but I am then stuck when trying to separate in to either 4 graphs or overlaid back-to-backs.
The code I was using for the overlay was ggplot with either geom_density or geom_histogram and the code for the back-to-back came from R-Bloggers and I used the first snippet:
ggplot(df, aes(IQ)) + geom_histogram(aes(x = x1, y = ..density..), fill = "blue") + geom_histogram( aes(x = x2, y = -..density..), fill = "green")
What I am looking for is a way to combine these two techniques or how to get ggplot to split the graphs up by factor in much the same was as plot/lattice does when you do, for example:
bwplot(y~x1.x2|Author, data=df)
The snippet that I am using to achieve separate plots includes facet_grid() such that the final code is:
ggplot(df, aes(y)) + facet_grid(~Author) + geom_histogram(aes(x = x1, y = ..density..), fill = "green") + geom_histogram(aes(x = x2, y = -..density..), fill = "blue")
I wasn't previously aware of the facet_grid() function of ggplot so thank you very much to MLavoie and Brandon Bertelsen.
This is very similar to this question (link), but I'm not quite sure how to manipulate it for my needs.
I have a faceted plot with two panels, and I would like to label three quadrants in the first panel and only the first panel.
Here is a mock data set:
dfr=data.frame(
variable=rep(c("A","B"),each=2),
x=c(2,-3,4,-5),
y=c(-2,4,-2,6))
And here is the plot:
p=ggplot(dfr,aes(x,y))+
geom_point()+
facet_grid(variable~.)+
scale_x_continuous(limits=c(-6,6))+
scale_y_continuous(limits=c(-6,6))+
geom_hline(yintercept=0)+
geom_vline(xintercept=0)
This is what I would like to accomplish:
You can always create a separate data frame with the desired labels and plot them using geom_text:
dfLab <- data.frame(variable = rep("A",3),
x = c(3,3,-3),
y = c(3,-3,-3),
lab = c('I','IV','III'))
ggplot(dfr,aes(x,y))+
geom_point()+
facet_grid(variable~.)+
scale_x_continuous(limits=c(-6,6))+
scale_y_continuous(limits=c(-6,6))+
geom_hline(yintercept=0)+
geom_vline(xintercept=0) +
geom_text(data = dfLab,aes(x=x,y=y,label=lab))