[![enter image description here][1]][1]I am using the following code to plot three sets of data and am also rearranging labels to a preferred order and making sure the 2 in R^2 is an exponent. As you can see the third panel of the facet wrap is empty. It should contain R2 (with 2 as exponent). I can't find the error that is leading to this in my code.
library(readxl)
library(ggplot2)
library(tidyr)
error3 <- read_excel('data/ggplot_error3.xlsx')
error3 <- transform(
error3,
Metric =as.factor(Metric),
Model =as.factor(Model),
Name =as.factor(Name),
Buffer=as.factor(Buffer)
)
levels(error3$Metric)
#Set facet wrap labels
my_labeller <- as_labeller(c( MAE = 'MAE', RMSE = 'RMSE', `R^2` = 'R^2'), default = label_parsed)
# Line plot with multiple groups
ggplot(data=error3, aes(x=Buffer, y=Value, group=Model)) +
theme_bw() +
geom_line(aes(color=Model)) +
geom_point(aes(color=Model)) +
labs(y = "Value", x = 'Buffer Radius (m)') +
facet_wrap(~ Metric, nrow = 1, scales = "free_y",
labeller=my_labeller)
# Reorder line plot so R2 comes last
library(tidyverse)
library(dplyr)
error3 %>% mutate(across(Metric, factor, levels=c('MAE','RMSE','R2'))) %>%
ggplot(aes(x=Buffer, y=Value, group=Model))+
geom_line(aes(color=Model)) +
geom_point(aes(color=Model)) +
theme_bw() +
labs(y = "Value", x = 'Buffer radius (m)') +
facet_wrap(~ Metric, nrow = 3, scales = "free_y", labeller=my_labeller)
#default level order
levels(error3$Model)
#change the order of labels
error3$Model <- factor(error3$Model, levels = c("RandomForest", "xgboost", "CART"))
#add superscript for R^2
error3 %>% mutate(across(Metric, factor, levels=c("MAE" , "RMSE", "R2"), labels = c('MAE', 'RMSE', 'R2'))) %>%
ggplot(aes(x=Buffer, y=Value, group=Model))+
geom_line(aes(color=Model)) +
geom_point(aes(color=Model)) +
theme_bw() +
labs(y = "Value", x = 'Buffer radius (m)') +
facet_grid(~ Metric, labeller=my_labeller) +
labs(color='Model name')
Code Sample:
error3 <-
structure(
list(
Buffer = structure(
c(
1L,
2L,
3L,
1L,
2L,
3L,
1L,
2L,
3L,
1L,
2L,
3L,
1L,
2L,
3L,
1L,
2L,
3L,
1L,
2L,
3L,
1L,
2L,
3L,
1L,
2L,
3L
),
.Label = c("500", "1000", "1500"),
class = "factor"
),
Area_km2 = c(
0.7854,
3.1416,
7.0686,
0.7854,
3.1416,
7.0686,
0.7854,
3.1416,
7.0686,
0.7854,
3.1416,
7.0686,
0.7854,
3.1416,
7.0686,
0.7854,
3.1416,
7.0686,
0.7854,
3.1416,
7.0686,
0.7854,
3.1416,
7.0686,
0.7854,
3.1416,
7.0686
),
Value = c(
0.626344682,
0.57916154,
0.684365417,
0.531505389,
0.49978974,
0.507811538,
0.5155205,
0.4966027,
0.4992761,
0.823620002,
0.80962234,
0.892280991,
0.710242935,
0.69967351,
0.699732972,
0.7211219,
0.7104436,
0.7156231,
0.197019417,
0.25163664,
0.09162464,
0.394737118,
0.43074958,
0.409713623,
0.3719265,
0.411827,
0.3833796
),
Metric = structure(
c(
1L,
1L,
1L,
1L,
1L,
1L,
1L,
1L,
1L,
3L,
3L,
3L,
3L,
3L,
3L,
3L,
3L,
3L,
2L,
2L,
2L,
2L,
2L,
2L,
2L,
2L,
2L
),
.Label = c("MAE", "R2", "RMSE"),
class = "factor"
),
Model = structure(
c(
1L,
1L,
1L,
2L,
2L,
2L,
3L,
3L,
3L,
1L,
1L,
1L,
2L,
2L,
2L,
3L,
3L,
3L,
1L,
1L,
1L,
2L,
2L,
2L,
3L,
3L,
3L
),
.Label = c("CART", "RandomForest", "xgboost"),
class = "factor"
),
Name = structure(
c(
1L,
1L,
1L,
2L,
2L,
2L,
3L,
3L,
3L,
7L,
7L,
7L,
8L,
8L,
8L,
9L,
9L,
9L,
4L,
4L,
4L,
5L,
5L,
5L,
6L,
6L,
6L
),
.Label = c(
"MAE_CART",
"MAE_rf",
"MAE_xgboost",
"R2_CART",
"R2_rf",
"R2_xgboost",
"RMSE_CART",
"RMSE_rf",
"RMSE_xgboost"
),
class = "factor"
)
),
class = "data.frame",
row.names = c(NA,-27L)
)
enter code here
As I already mentioned in my comment the R^2 is coded as R2 in your Metric column. Hence you could fix your issue using as_labeller(c(..., R2 = "R^2")):
library(ggplot2)
library(tidyr)
library(dplyr)
my_labeller <- as_labeller(c(MAE = "MAE", RMSE = "RMSE", R2 = "R^2"), default = label_parsed)
error3 <- error3 %>% mutate(across(Metric, factor, levels = c("MAE", "RMSE", "R2")))
ggplot(data = error3, aes(x = Buffer, y = Value, group = Model)) +
theme_bw() +
geom_line(aes(color = Model)) +
geom_point(aes(color = Model)) +
labs(y = "Value", x = "Buffer Radius (m)") +
facet_wrap(~Metric,
nrow = 1, scales = "free_y",
labeller = my_labeller
)
Related
I have a data like this and I want to save figures one after another by
df<- structure(list(x = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L), rn = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("AAAAA",
"BBBBB", "CCCCC", "DDDDD"), class = "factor"), key = structure(c(2L,
4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L), .Label = c("WSAfrica",
"Wscanada", "WSInida", "WSUSA"), class = "factor"), Median = c(0.000621,
0.000777, 0.000574, 0.000537, 0.000381, 0.00177, 0.002, 0.000457,
0.00247, 0.00199, 0.00287, 0.00224, 5.94e-05, 4.12e-05, 4.44e-05,
5.68e-05), SD = c(0.000127453, 0.000107802, 0.001048659, 9.32e-05,
9.23e-05, 0.000120554, 0.000914697, 0.000167046, 0.000125033,
0.000410528, 0.000450444, 0.000310483, 5.91e-06, 8.98e-06, 1.11e-05,
1.16e-05)), class = "data.frame", row.names = c(NA, -16L))
I tried the following function but I get an error saying that
Error in UseMethod("grid.draw") : no applicable method for
'grid.draw' applied to an object of class "function"
d_ply(df, .(rn),
function(x) (ggplot(x, aes(x = x, y = Median))+
geom_point() +
scale_x_discrete(limit = c("Wscanada", "WSUSA", "WSInida","WSAfrica")) +
geom_errorbar(aes(ymin = Median-SD, ymax = Median+SD))+
ggsave(., filename = paste0("", x$rn[1],".pdf"))))
You're already adding the plot to ggsave, no need to use the . for the first argument:
d_ply(df, .(rn),
function(x) (ggplot(x, aes(x = x, y = Median))+
geom_point() +
scale_x_discrete(limit = c("Wscanada", "WSUSA", "WSInida","WSAfrica")) +
geom_errorbar(aes(ymin = Median-SD, ymax = Median+SD))+
ggsave(filename = paste0("", x$rn[1],".pdf"))))
UPDATED:
Data has now been updated to full chemistry values as opposed to mean values.
I am attempting to create a box and whisker plot in r, on a very small dataset. My data is not behaving itself or I am missing some glaringly obvious error.
This is the code i have for making said plot
library(ggplot2)
Methanogenesis_Data=read.csv("CO2-CH4 Rates.csv")
attach(Methanogenesis_Data)
summary(Methanogenesis_Data)
str(Methanogenesis_Data)
boxplot(CH4rate~Patch+Temperature, data = Methanogenesis_Data,
xlab="Patch", ylab="CH4 Production")
cols<-c("red", "blue")
From this small dataset.
structure(list(Patch = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Gravel", "Macrophytes",
"Marginal"), class = "factor"), Temperature = structure(c(2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Cold",
"Warm"), class = "factor"), CH4rate = c(0.001262595, 0.00138508,
0.001675944, 0.001592354, 0.002169233, 0.001772964, 0.002156633,
0.002864403, 0.002301383, 0.002561042, 0.005189598, 0.004557227,
0.008484851, 0.006867866, 0.007438633, 0.005405327, 0.006381582,
0.008860084, 0.007615417, 0.007705906, 0.009198508, 0.00705233,
0.007943024, 0.008319768, 0.010362114, 0.007822153, 0.010339339,
0.009252302, 0.008249555, 0.008197657), CO2rate = c(0.002274825,
0.002484866, 0.003020209, 0.00289133, 0.003927232, 0.003219346,
0.003922613, 0.005217026, 0.00418674, 0.00466427, 0.009427322,
0.008236453, 0.015339532, 0.012494729, 0.013531303, 0.009839847,
0.011624428, 0.016136746, 0.0138831, 0.014051034, 0.016753211,
0.012780956, 0.01445912, 0.01515584, 0.01883252, 0.014249452,
0.018849478, 0.016863299, 0.015045964, 0.014941168)), .Names = c("Patch",
"Temperature", "CH4rate", "CO2rate"), class = "data.frame", row.names =
c(NA,
-30L))
The plot I get as output is good, however I would like the Variables on the X axis to simply display "Gravel" "Macrophytes" "Marginal" as opposed to each of those variables with Warm and Cold. Thanks for any assistance
THIS IS WHAT I AM TRYING TO ACHEIVE -----> Exact Boxplot I want to create
Following your update with an example graph :
I have also included the formating for the legend position. If you want to edit the y axis label to include subscript I would suggest you read over this. I have included a blank title for relabelling.
test <- structure(list(Patch = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Gravel", "Macrophytes",
"Marginal"), class = "factor"), Temperature = structure(c(2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Cold",
"Warm"), class = "factor"), CH4rate = c(0.001262595, 0.00138508,
0.001675944, 0.001592354, 0.002169233, 0.001772964, 0.002156633,
0.002864403, 0.002301383, 0.002561042, 0.005189598, 0.004557227,
0.008484851, 0.006867866, 0.007438633, 0.005405327, 0.006381582,
0.008860084, 0.007615417, 0.007705906, 0.009198508, 0.00705233,
0.007943024, 0.008319768, 0.010362114, 0.007822153, 0.010339339,
0.009252302, 0.008249555, 0.008197657), CO2rate = c(0.002274825,
0.002484866, 0.003020209, 0.00289133, 0.003927232, 0.003219346,
0.003922613, 0.005217026, 0.00418674, 0.00466427, 0.009427322,
0.008236453, 0.015339532, 0.012494729, 0.013531303, 0.009839847,
0.011624428, 0.016136746, 0.0138831, 0.014051034, 0.016753211,
0.012780956, 0.01445912, 0.01515584, 0.01883252, 0.014249452,
0.018849478, 0.016863299, 0.015045964, 0.014941168)), .Names = c("Patch",
"Temperature", "CH4rate", "CO2rate"), class = "data.frame", row.names =
c(NA,
-30L))
Now I will create two data sets one for each graph just for simplicity you could leave them combined and facet but for formatting purposes this might be easier.
CH4rate <- test %>%
gather("id", "value", 3:4) %>%
filter(id == "CH4rate")
CO2rate <- test %>%
gather("id", "value", 3:4) %>%
filter(id == "CO2rate")
First plot:
ggplot(CH4rate) +
geom_boxplot(mapping = aes(x = Patch, y = value, fill=factor(Temperature, levels = c("Warm", "Cold")))) +
theme(legend.position = c(0.15, 0.9), panel.background = element_rect(fill = "white", colour = "grey50")) +
labs(title = "Title of graph", x="Patch Type", y = "CH4rate") +
scale_fill_manual(name = "", values = c("orange", "light blue")
, labels = c("Cold" = "Incubated at 10˙C", "Warm" = "Incubated at 26˙C"))
Second plot:
ggplot(CO2rate) +
geom_boxplot(mapping = aes(x = Patch, y = value, fill=factor(Temperature, levels = c("Warm", "Cold")))) +
theme(legend.position = c(0.15, 0.9), panel.background = element_rect(fill = "white", colour = "grey50")) +
labs(title = "Title of graph", x="Patch Type", y = "CO2rate") +
scale_fill_manual(name = "", values = c("orange", "light blue")
, labels = c("Cold" = "Incubated at 10˙C", "Warm" = "Incubated at 26˙C"))
I have the following graph:
And would like to make what I thought would be a very simple change: I would like to remove the top, right and bottom sides of the left facet label border lines.
How do I do I remove those lines, or draw the equivalent of the right hand lines? I would rather not muck about with grobs, if possible, but won't say no to any solution that works.
Graph code:
library(ggplot2)
library(dplyr)
library(forcats)
posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
strip.background.x=element_rect(color = NA, fill=NA),
strip.background.y=element_rect(color = "black", fill=NA)) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
Data:
structure(list(Dataset = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 5L, 5L, 5L, 5L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L), .Label = c("All.habitat", "Aut.habitat", "Habitat.season",
"Lit.season", "Spr.habitat"), class = "factor"), Species = structure(c(1L,
2L, 3L, 5L, 6L, 10L, 11L, 12L, 13L, 1L, 3L, 5L, 6L, 13L, 1L,
2L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L), .Label = c("Ar.sp1",
"Ar.sp2", "Arc.sp1", "B.pus", "Dal.sp1.bumps", "Dip.unID", "I.palladium",
"Pale", "Ph.sp3", "Port", "Somethus", "sty", "Sty.sp1"), class = "factor"),
Species2 = structure(c(2L, 9L, 1L, 4L, 5L, 7L, 11L, 12L,
13L, 2L, 1L, 4L, 5L, 13L, 2L, 9L, 4L, 5L, 6L, 10L, 8L, 7L,
11L, 13L), .Label = c("Arcitalitrus sp1", "Armadillidae sp1 ",
"Brachyiulus pusillus ", "Dalodesmidae sp1", "Diplopoda",
"Isocladosoma pallidulum ", "Ommatoiulus moreleti ", "Philosciidae sp2",
"Porcellionidae sp1", "Siphonotidae sp2", "Somethus sp1",
"Styloniscidae ", "Styloniscidae sp1"), class = "factor"),
Taxa = structure(c(3L, 3L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
1L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L), .Label = c("Amphipoda",
"Diplopoda", "Isopoda"), class = "factor"), Variable = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Autumn", "Litter",
"Spring", "Summer"), class = "factor"), Coef = c(1.911502938,
2.086917154, 1.571872993, 12.61184801, 15.6161116, -1.430032837,
-12.51944478, 12.33934516, -8.040249562, 8.08258816, 1.780142396,
12.88982576, 16.78107544, -13.22641153, 1.68810887, 2.093965381,
12.27209197, 15.08328526, -6.334640911, -11.29985948, -11.62658947,
-1.676293808, -6.246555908, -3.470297147), SE = c(0.403497472,
2.21607562, 0.348600794, 2.423896379, 0.509468128, 3.423013791,
2.382857733, 1.775086895, 2.087788334, 2.23631504, 0.33402261,
2.518562443, 0.459720131, 1.950974996, 0.2476205, 0.235648095,
1.815155489, 0.325804415, 2.564680067, 2.437104984, 2.212583358,
2.677618401, 2.324019051, 0.420436743), Test.stat = c(18.36532749,
13.27324683, 13.29039037, 20.50277493, 44.06097153, 10.55234932,
14.64951518, 13.22575401, 20.16415411, 16.55627107, 11.81407568,
15.15213717, 40.67205188, 12.62233207, 37.60085488, 16.90879258,
20.20215107, 80.30520371, 13.35250626, 13.01692428, 17.52987519,
20.03658771, 12.02467914, 53.5052683)), row.names = 10:33, class = "data.frame")
This solution is based on grobs: find positions of "strip-l" (left strips) and then substitute the rect grobs with line grobs.
p <- posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
#strip.background.x=element_rect(color = "white", fill=NULL),
strip.background.y=element_rect(color = NA)
) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
library(grid)
q <- ggplotGrob(p)
lg <- linesGrob(x=unit(c(0,0),"npc"), y=unit(c(0,1),"npc"),
gp=gpar(col="red", lwd=4))
for (k in grep("strip-l",q$layout$name)) {
q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg
}
grid.draw(q)
I have a dataset of >100 different samples. Samples are from different genotypes (e.g. X, Y, Z) and 4 different time points (T0,1,2,3) with 3 biological replicates (R1,2,3). I'm measuring values for 50 different genes (in rows; A,B..)
longdata <- structure(list(Gene = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("A", "B"), class = "factor"), Genotype = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("X", "Y", "Z"), class = "factor"),
Time = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("T0",
"T1", "T2", "T3"), class = "factor"), Ave = c(1.32606106633333,
1.499956424, 1.118528738, 1.025082136, 0.424537206666667,
0.723243112666667, 0.335509156333333, 0.328275209, 0.788329993666667,
1.125292329, 2.357924224, 0.678921448, 0.222768019, 0.293117217,
0.548228048, 0.841192647333333, 3.144197864, 0.576764958333333,
1.32037215366667, 1.15039119233333, 1.03539976366667, 1.00032109266667,
0.740699933666667, 0.687992671666667), SE = c(0.119785209010494,
0.168580466330281, 0.264739468221289, 0.124588107424543,
0.194995686650518, 0.0392007703821249, 0.06203362889702,
0.0482287534807508, 0.396968455138007, 0.0903480171168777,
0.717823561374135, 0.164024037188693, 0.0078580995264886,
0.0980939303386436, 0.233081861930954, 0.0870744069976396,
0.324195222544884, 0.434640930315622, 0.0658409437053185,
0.135850334794207, 0.175517934316736, 0.123213160632528,
0.133598346586129, 0.203707785326976)), .Names = c("Gene",
"Genotype", "Time", "Ave", "SE"), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -24L))
How can I modify this syntax to generate each graph separately and save them as JPG/PNG files?
longdata %>% ggplot(aes(x = Time, y = Ave, fill = Genotype)) + geom_bar(position = position_dodge(), stat = "identity") + geom_errorbar(aes(ymin = Ave - SE, ymax = Ave + SE), width = 0.1, position = position_dodge(0.9)) + facet_wrap(~ Gene)
You can put ggplot and ggsave within a loop.
lapply(sort(unique(longdata$Gene)), function(i){
ggplot(longdata[longdata$Gene == i, ], aes(x = Time, y = Ave, fill = Genotype)) + geom_bar(position = position_dodge(), stat = "identity") + geom_errorbar(aes(ymin = Ave - SE, ymax = Ave + SE), width = 0.1, position = position_dodge(0.9))
ggsave(filename = paste0(i, ".png"))
})
This loop gets the unique elements of Gene, sorts them, create a plot, then save the result.
I have a data frame lot_main that looks like this:
I want to make a bar plot with columns reorders according to the wordcount.
library(ggplot2)
library(viridis)
lotr_main %>% ggplot(aes(x = Character, y = wordcount, fill = Film)) +
geom_bar(stat="identity",position = "dodge") +
coord_flip() +
scale_fill_viridis("Film",discrete = TRUE, option = "C")
The plot I got:
What I want is for each character, the bars are reorders with the longest on the top and shortest at the bottom. The orders of the bars don't need to be the same for each character.
You essentially want to fill by one thing, and order by another. A solution is thus to pry them apart and create a separate 'order' variable. Of note, I don't know if sorting your bars by value instead of having the same sequence each 'group' makes your plot more understandable.....
create some data:
library(data.table)
set.seed(123)
dat <- expand.grid(group=LETTERS[1:3],
subgroup=LETTERS[1:3])
dat$value <- runif(nrow(dat))
setDT(dat)
Create the order variable:
dat[,order:=order(value),by=group]
Create the plot
p1 <- ggplot(dat, aes(x=group,y=value, fill=subgroup,group=order))+
geom_bar(aes(group=order),position="dodge", stat="identity") +
coord_flip()
p1
Here's a start. Found data and inspiration here (code below)
LoTRdata <- structure(list(Film = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("The Fellowship Of The Ring",
"The Return Of The King", "The Two Towers"), class = "factor"),
Race = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L,
3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Elf", "Hobbit",
"Man"), class = "factor"), Gender = structure(c(1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), .Label = c("Female", "Male"), class = "factor"), Words = c(1229L,
971L, 14L, 3644L, 0L, 1995L, 331L, 513L, 0L, 2463L, 401L,
3589L, 183L, 510L, 2L, 2673L, 268L, 2459L)), .Names = c("Film",
"Race", "Gender", "Words"), class = "data.frame", row.names = c(NA,
-18L))
LoTRdataOrder <- LoTRdata[order(LoTRdata$Words, LoTRdata$Film) , ]
# install.packages("ggplot2", dependencies = TRUE)
require(ggplot2)
p <- ggplot(LoTRdataOrder, aes(x = Race, y = Words, fill = Film))
p + geom_bar(stat = "identity", position = "dodge") +
coord_flip() + guides(fill = guide_legend())