Remove space between bars within a grid - r

I want to move bars that are within one grid closer to save space. Is there a way to do that? The code I used just change the bar width and does not change the spacing. I am aware that bindwidth no longer work in geom_bar as I referred to these problems:
Adding space between bars in ggplot2
Increase space between bars in ggplot
This is the data
dput(grp1)
structure(list(Rot = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2-year",
"3-year", "4-year"), class = "factor"), Rot.Herb = structure(c(3L,
4L, 13L, 14L, 5L, 6L, 9L, 10L, 15L, 16L, 1L, 2L, 7L, 8L, 11L,
12L, 17L, 18L), .Label = c("A4-conv", "A4-low", "C2-conv", "C2-low",
"C3-conv", "C3-low", "C4-conv", "C4-low", "O3-conv", "O3-low",
"O4-conv", "O4-low", "S2-conv", "S2-low", "S3-conv", "S3-low",
"S4-conv", "S4-low"), class = "factor"), Rot.trt = structure(c(2L,
2L, 7L, 7L, 3L, 3L, 5L, 5L, 8L, 8L, 1L, 1L, 4L, 4L, 6L, 6L, 9L,
9L), .Label = c("A4", "C2", "C3", "C4", "O3", "O4", "S2", "S3",
"S4"), class = "factor"), Crop = structure(c(2L, 2L, 4L, 4L,
2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("alfalfa",
"corn", "oat", "soybean"), class = "factor"), Year = c(2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Herb.trt = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("conv", "low"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = "kg.ha", class = "factor"), N = c(4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), value = c(0.43168573275,
16.22463846275, 0.554446363, 25.89844593075, 13.882743364, 84.1922080725,
84.2625, 105.7375, 0.8350420895, 106.7464386085, 127.8875, 65.3875,
1.964170084, 29.46524929925, 249.7625, 333.125, 0.535560112,
28.757014893), sd = c(0.528135488290589, 20.3216237207314, 0.8810357408252,
40.4576156325903, 27.3195114207629, 71.6424882235524, 22.6948736869511,
40.9587266851237, 0.931926987921512, 108.45555847826, 89.1035387905553,
25.141843442092, 2.20017281317418, 24.1072626208021, 93.2983509589889,
85.0439543216722, 0.48339034731648, 33.2628789566726), se = c(0.264067744145294,
10.1608118603657, 0.4405178704126, 20.2288078162951, 13.6597557103815,
35.8212441117762, 11.3474368434756, 20.4793633425619, 0.465963493960756,
54.2277792391299, 44.5517693952777, 12.570921721046, 1.10008640658709,
12.0536313104011, 46.6491754794945, 42.5219771608361, 0.24169517365824,
16.6314394783363), ci = c(0.840381416699796, 32.3362381637038,
1.40192446910603, 64.3770946952626, 43.4714390915815, 113.999185974188,
36.112608456959, 65.1744742040986, 1.48290379975249, 172.576995683309,
141.783613905853, 40.0062833851536, 3.50096592013592, 38.3600344290379,
148.458496149248, 135.323909108861, 0.76918191241357, 52.928663119381
)), .Names = c("Rot", "Rot.Herb", "Rot.trt", "Crop", "Year",
"Herb.trt", "variable", "N", "value", "sd", "se", "ci"), row.names = c(NA,
-18L), class = "data.frame")
This is the code
ggplot(grp1, aes(x=Rot.Herb, y=value, fill=factor(Herb.trt)))+
geom_bar(stat="identity", position=position_dodge(.), width=.5)+
scale_fill_brewer(palette = "Dark2")+
theme_bw() +
theme(panel.grid.major=element_blank()) +
facet_grid(~Crop, scales = "free_x", space="free_x")+
theme(legend.title=element_blank(),legend.text=element_text(size=20),legend.position="top")+
geom_errorbar(aes(ymin=value-se, ymax=value+se), size=0.75, width=.25,position=position_dodge(.5))+
xlab("Treatment") +
theme(axis.title = element_text(size=24,face="bold", vjust=4)) +
ylab("2014 total weed biomass (Kg/ha)\n") +
theme(axis.title = element_text(size=24,face="bold", vjust=2), axis.text.y = element_text(size=20, color="black"))+
theme(strip.text.x = element_text(colour = "black", size = 15), strip.background = element_rect(fill = "white"), axis.text.x = element_blank(), axis.ticks.x = element_blank())
And graph
What I want is a graph with bar width of 0.5 and no spacing between bars in the same grid. Thank you for all the input.

I think you want width = 0.5 while keeping the bars touching because you don't want the absolute width to change (width = 0.5 changes the relative width). This might be closer to what you are asking for.
ggplot(grp1, aes(x=Rot.Herb, y=value, fill=factor(Herb.trt)))+
geom_bar(stat="identity", position = "dodge", width=1)+
scale_x_discrete(expand=c(0.2,0))+
scale_fill_brewer(palette = "Dark2")+
theme_bw() +
theme(panel.grid.major=element_blank()) +
facet_grid(~Crop, scales = "free_x", space="free_x")+
theme(legend.title=element_blank(),legend.text=element_text(size=20),legend.position="top")+
geom_errorbar(aes(ymin=value-se, ymax=value+se), size=0.75, width=.25,position=position_dodge(0.5))+
xlab("Treatment") +
theme(axis.title = element_text(size=24,face="bold", vjust=4)) +
ylab("2014 total weed biomass (Kg/ha)\n") +
theme(axis.title = element_text(size=24,face="bold", vjust=2), axis.text.y = element_text(size=20, color="black"))+
theme(strip.text.x = element_text(colour = "black", size = 15), strip.background = element_rect(fill = "white"), axis.text.x = element_blank(), axis.ticks.x = element_blank())
Basically I changed the width = 1 as suggested by #Gregor, which causes the bars to touch, but added an extra option scale_x_discrete(expand=c(0.2,0)) to add spaces between the bars and the sides of the plot. This in effect adjusts the absolute width of the bars while keeping them touching. The first number gives the multiplicative constant used to expand the range, while the second gives the additive.
You can experiment with the two values in expand = to get what you want.
Also see this related question: ggplot geom_boxplot: reduce space between x-axis categories

Related

Customize x axis tick in a bar graph with a factor different than the x factor in aes command

I am making this graph with x = Rot.Herb and y = kg.ha. I want to label the ticks on the x axis with Rot instead of Rot.Herb.
Below is the code that I used, learning from this problem
Overflowing X axis (ggplot2)
ggplot(grp1, aes(x=Rot.Herb, y=value, fill=factor(Herb.trt)))+
geom_bar(stat="identity", position="dodge", width=1)+
scale_x_discrete(expand=c(0.2,0),
labels = as.character(Rot))+
scale_fill_manual(values=c("#EEAD0E","#BB0000"), labels=c("Conventional","Low"))+
theme_bw() +
theme(panel.grid.major=element_blank()) +
facet_grid(~Crop, scales = "free_x", space="free_x")+
theme(legend.title=element_blank(),legend.text=element_text(size=15),legend.position=c(.3,.8))+
geom_errorbar(aes(ymin=value-se, ymax=value+se), size=0.75, width=.25,position=position_dodge(.5))+
xlab("Rotation") +
theme(axis.title = element_text(size=24,face="bold", vjust=4), axis.text.x = element_text(size=20, color="black")) +
ylim(c(0,1500))+
ylab("Total weed biomass (Kg/ha)\n") +
theme(axis.title = element_text(size=24,face="bold", vjust=2), axis.text.y = element_text(size=20, color="black"))+
theme(strip.text.x = element_text(colour = "black", size = 15), strip.background = element_rect(fill = "white"))
and R returns an error as I say
scale_x_discrete(expand=c(0.2,0),
labels = as.character(Rot))
The error is
Error in check_breaks_labels(breaks, labels) : object 'Rot' not found
Here is my data
> dput(grp1)
structure(list(Rot = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2-year",
"3-year", "4-year"), class = "factor"), Rot.Herb = structure(c(3L,
4L, 13L, 14L, 5L, 6L, 9L, 10L, 15L, 16L, 1L, 2L, 7L, 8L, 11L,
12L, 17L, 18L), .Label = c("A4-conv", "A4-low", "C2-conv", "C2-low",
"C3-conv", "C3-low", "C4-conv", "C4-low", "O3-conv", "O3-low",
"O4-conv", "O4-low", "S2-conv", "S2-low", "S3-conv", "S3-low",
"S4-conv", "S4-low"), class = "factor"), Rot.trt = structure(c(2L,
2L, 7L, 7L, 3L, 3L, 5L, 5L, 8L, 8L, 1L, 1L, 4L, 4L, 6L, 6L, 9L,
9L), .Label = c("A4", "C2", "C3", "C4", "O3", "O4", "S2", "S3",
"S4"), class = "factor"), Crop = structure(c(2L, 2L, 4L, 4L,
2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("alfalfa",
"corn", "oat", "soybean"), class = "factor"), Year = c(2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Herb.trt = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("conv", "low"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = "kg.ha", class = "factor"), N = c(4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), value = c(0.43168573275,
16.22463846275, 0.554446363, 25.89844593075, 13.882743364, 84.1922080725,
84.2625, 105.7375, 0.8350420895, 106.7464386085, 127.8875, 65.3875,
1.964170084, 29.46524929925, 249.7625, 333.125, 0.535560112,
28.757014893), sd = c(0.528135488290589, 20.3216237207314, 0.8810357408252,
40.4576156325903, 27.3195114207629, 71.6424882235524, 22.6948736869511,
40.9587266851237, 0.931926987921512, 108.45555847826, 89.1035387905553,
25.141843442092, 2.20017281317418, 24.1072626208021, 93.2983509589889,
85.0439543216722, 0.48339034731648, 33.2628789566726), se = c(0.264067744145294,
10.1608118603657, 0.4405178704126, 20.2288078162951, 13.6597557103815,
35.8212441117762, 11.3474368434756, 20.4793633425619, 0.465963493960756,
54.2277792391299, 44.5517693952777, 12.570921721046, 1.10008640658709,
12.0536313104011, 46.6491754794945, 42.5219771608361, 0.24169517365824,
16.6314394783363), ci = c(0.840381416699796, 32.3362381637038,
1.40192446910603, 64.3770946952626, 43.4714390915815, 113.999185974188,
36.112608456959, 65.1744742040986, 1.48290379975249, 172.576995683309,
141.783613905853, 40.0062833851536, 3.50096592013592, 38.3600344290379,
148.458496149248, 135.323909108861, 0.76918191241357, 52.928663119381
)), .Names = c("Rot", "Rot.Herb", "Rot.trt", "Crop", "Year",
"Herb.trt", "variable", "N", "value", "sd", "se", "ci"), row.names = c(NA,
-18L), class = "data.frame")
and my graph
The graph that I want should say "4-year" at the tick in between the two bars in the Alfalfa grid.

Problems with geom_tile and scale_colour_distiller

I want to create a correlation plot using geom_tile(). This is a reproducible chunk of code:
library(ggplot2)
df.m <- structure(list(Trait = structure(c(6L, 5L, 1L, 3L, 2L, 9L, 4L,
10L, 11L, 7L, 8L, 6L, 5L, 1L, 3L, 2L, 9L, 4L, 10L, 11L, 7L, 8L,
6L, 5L, 1L, 3L, 2L, 9L, 4L, 10L, 11L, 7L, 8L), .Label = c("R1",
"R2", "R3", "R4", "R5",
"R6", "R7", "R8",
"R9", "R10",
"R11"), class = "factor"), Variable = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("C1", "C2", "C3"), class = "factor"),
value = c(0.967444360256195, 0.937379062175751, 0.647411823272705,
0.512605130672455, 0.50750744342804, 0.508640229701996, 0.508640229701996,
0.503542542457581, 0.442936152219772, 0.510905921459198,
0.504675328731537, NA, 0.834005177021027, 0.667642116546631,
0.579914391040802, 0.579344689846039, 0.536050498485565,
0.532062888145447, 0.52408766746521, 0.520099997520447, 0.504719197750092,
0.450031787157059, NA, NA, 0.571457028388977, 0.451690584421158,
0.354736804962158, 0.46138596534729, 0.477354824542999, 0.447128057479858,
0.287439465522766, 0.498456537723541, 0.508722245693207)), .Names = c("Trait",
"Variable", "value"), row.names = c(NA, -33L), class = "data.frame")
p <- ggplot(na.omit(df.m)) + aes(x = Variable, y = Trait, fill = value) +
geom_tile(colour = "white", size = 0.75) +
scale_colour_distiller(limits = c(-1, 1), direction = -1, palette = "RdBu", name = "Coefficient") +
scale_size(range = c(0,20), name = title, guide = "none") +
geom_text(data = df.m, aes(x = Variable, y = Trait, label = sprintf("%.2f", value)), size = 4, inherit.aes = FALSE) +
theme(plot.title = element_text(size = 20), axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 10, angle = 90, hjust = 1, vjust = 0.5),
legend.position = "bottom", legend.margin = unit(1.0, "cm"),
legend.text = element_text(size = 8), legend.title = element_text(size = 10),
legend.key.size = unit(1.0, "cm"),
panel.background = element_rect(fill = "white")) +
xlab("\nVariables 1") + ylab("Variables 2\n")
plot(p)
However, the tiles are filled with their own color gradient scale and not with the one defined bye scale_colour_distiller(). Moreover, both key legends appear in the plot:
I just want the tiles to be colored according to the gradient defined by the scale_colour_distiller(). How can I do that? From where geom_plot() is sucking the color scale?
Thanks in advance!
You need to use scale_fill_distiller since you are mapping value to the fill aesthetic and not to the color aesthetic.

ggplot: how to change colors of vertical lines according to group id (in polar plot)

I have the following dataframe (output of dput(df2)):
structure(list(angles = c(-0.701916320805404, 2.33367948606366,
0.364313791379516, -0.228918909875176, -2.77064550417737, 2.97776037032614,
-3.03604124258522, 2.10507549390108, 2.07708771915781, -0.0646656487453258,
-0.701916320805404, 2.33367948606366, 0.364313791379516, -0.228918909875176,
-2.77064550417737, 2.97776037032614, -3.03604124258522, 2.10507549390108,
2.07708771915781, -0.0646656487453258, -0.701916320805404, 2.33367948606366,
0.364313791379516, -0.228918909875176, -2.77064550417737, 2.97776037032614,
-3.03604124258522, 2.10507549390108, 2.07708771915781, -0.0646656487453258
), id = c(9L, 4L, 5L, 6L, 3L, 10L, 3L, 4L, 4L, 6L, 1L, 4L, 5L,
6L, 2L, 1L, 3L, 4L, 4L, 6L, 1L, 7L, 5L, 6L, 2L, 1L, 3L, 4L, 4L,
6L), method = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("kd-clips", "QT-Clust", "True"
), class = "factor"), truid = structure(c(1L, 4L, 5L, 6L, 2L,
1L, 3L, 4L, 4L, 6L, 1L, 4L, 5L, 6L, 2L, 1L, 3L, 4L, 4L, 6L, 1L,
4L, 5L, 6L, 2L, 1L, 3L, 4L, 4L, 6L), .Label = c("1", "2", "3",
"4", "5", "6"), class = "factor")), .Names = c("angles", "id",
"method", "truid"), row.names = c(940L, 474L, 889L, 298L, 222L,
932L, 87L, 695L, 261L, 832L, 1940L, 1474L, 1889L, 1298L, 1222L,
1932L, 1087L, 1695L, 1261L, 1832L, 2940L, 2474L, 2889L, 2298L,
2222L, 2932L, 2087L, 2695L, 2261L, 2832L), class = "data.frame")
I run the following code to make the plot that follows:
df2$y <- as.numeric(as.factor(df2$method)) + 3
df2$yend <- df2$y + 1
library(ggplot2)
library(RColorBrewer)
cx <- ggplot(df2, aes(y = y, x = angles))
cx + geom_point(aes(color = as.factor(id))) + ylim(0,6) + theme_light() + scale_colour_brewer(palette = "Paired") +
scale_x_continuous(labels = NULL, breaks = df2$angles)+coord_polar() +
theme(legend.position="none", panel.border=element_blank(), axis.title =
element_blank(), axis.text = element_blank())
I get the following figure:
Almost there, but what I would like to get are two more things:
The radial lines to be colored according to the last column (true.id) of the df2 (which is the same color as the points in the third concentric circle -- same as that for id == "True").
I would like a radial scale also, marked at intervals of 30 (like at angles of 0, 30, 60, 90, ... 330. 0). However, I do not want the scale at the left (of the y's).
The above has 30 points, three replicates of each method at each angle. However, the figures only appears to plot 9 replicates, i.e. 27 points in total. (It is possible that two angles -- the one with 2.077 and the one with 2.105) are very close, so that they are really perhaps both there, but I can not tell because then what are the two points that are close to each other?
I have tried all day but could not get either of these to work, so I was wondering if anyone can help.
Thanks in advance!
I think this is probably what you wanted (I know, long time ago now...). Some of what you were getting wrong I actually couldn't quite tell, but it's nearly always a mistake to try to use the coordinate gridlines to show data - use geom_line or geom_segment for the data, and leave the gridlines to show the coordinates. This solves both your need to have the lines coloured, and makes it easier for the x gridlines (ie the radial ones) to have the labels you want (whether I understood you correctly on this, re degrees v. radians, I'm not sure).
library(ggplot2)
library(RColorBrewer)
# your "angle" looks to be in radians, not sure how you want these converted to degrees?
# but let's set where we want the axis marks to be
br <- seq(from = -pi, to = pi, length.out = 7)
ggplot(df2, aes(y = y, x = angles)) +
geom_point(aes(color = as.factor(id))) +
theme_light() +
scale_colour_brewer(palette = "Paired") +
geom_vline(aes(xintercept = angles, colour = truid)) +
coord_polar() +
scale_x_continuous(lim = c(-pi, pi), breaks = br, labels = 0:6 * 60) +
theme(legend.position="none",
panel.border=element_blank(),
axis.title = element_blank(),
axis.text.y = element_blank())

How to center values in stackbar plot and add greek text to legend with ggplot

I have created a stacked-bar plot with 'ggplot' to display my karyotype (molecular) results from a transplant experiment, with each panel representing a location, and the x-axis is the various substrates, while the y-axis is the percentage of each of the three karyotypes.
I have looked over several examples of questions and answers from Stack Overflow and cannot figure out how to do the following:
centre the values (should be rounded to two decimal places) within each section of the stacked bars, right now I just have them offset from the top.
how to change my legend text from "BB" to the Greek "lower alpha, lower alpha", "BD" to Greek "lower alpha, lower beta", and "DD" to Greek "lower beta, lower beta".
Here is some sample data and code with a copy of the plot it generates.
Karotype.Data <- structure(list(Location = structure(c(1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 1L, 4L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Kampinge", "Kaseberga", "Molle", "Steninge"), class = "factor"), Substrate = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 4L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 1L, 4L, 2L), .Label = c("Kampinge", "Kaseberga", "Molle", "Steninge"), class = "factor"), Karyotype = structure(c(1L, 3L, 4L, 1L, 3L, 3L, 4L, 4L, 4L, 3L, 1L, 4L, 3L, 4L, 2L, 3L, 1L, 4L, 3L, 2L, 4L, 3L, 4L, 2L, 3L), .Label = c("", "BB", "BD", "DD"), class = "factor")), .Names = c("Location", "Substrate", "Karyotype"), row.names = c(135L, 136L, 137L, 138L, 139L, 165L, 166L, 167L, 168L, 169L, 236L, 237L, 238L, 239L, 240L, 326L, 327L, 328L, 329L, 330L, 426L, 427L, 428L, 429L, 430L), class = "data.frame")
z.counts <- Karotype.Data %>%
group_by(Location,Substrate,Karyotype) %>%
summarise(Frequency=n())
z.freq <- z.counts %>% filter(Karyotype != '') %>%
group_by(Location,Substrate) %>%
mutate(Percent=Frequency/sum(Frequency))
z.freq
ggplot(z.freq, aes(x=Substrate, y=Percent, fill=Karyotype )) +
geom_bar(stat="identity") +
geom_text(aes(label = Percent), size = 5, vjust = 1, position = "stack") +
facet_wrap(~ Location, ncol=2) +
scale_y_continuous(name="Percentage") +
theme(strip.text.x = element_text(colour="black", size=20, face="bold"),
axis.title.x = element_text(colour="black", size=20, face="bold", vjust=-0.5),
axis.text.x = element_text(colour="black", size=18),
axis.title.y = element_text(colour="black", size=20,face="bold", vjust=1),
axis.text.y = element_text(colour="black", size=18),
legend.title = element_text(colour="black", size=20, face="bold"),
legend.text = element_text(colour="black", size = 18),
legend.position="bottom")
To add greek letters to the legend, you can change the colour scale with scale_colour_manual():
test = data.frame(x=1:30,y=1:30,label=rep(c("BB","BD","DD"),each=10))
ggplot(test) + geom_point(aes(x=x,y=y,color=label)) + scale_colour_manual(values=c(1,2,3),breaks = c("BB","BD","DD"),labels = list(bquote(alpha~alpha),bquote(alpha~beta),bquote(beta~beta)))
argument values sets the colour, breaks sets your breakpoints (BB, BD and DD) and labels sets the greek letters you want.
To round the legend, you can add another column to your dataframe, setting values to round(Percent,digits=3), and use this column in the geom_text.
Informations concerning greek letters in ggplot2 can be found here

Plot histogram in ggplot

I have this dataframe.
dput(EF_Lat_Am)
structure(list(V1 = structure(c(4L, 3L, 5L, 6L, 1L, 2L, 7L, 8L,
4L, 3L, 5L, 6L, 1L, 2L, 7L, 8L), .Label = c("Crop Agriculture",
"Mining", "Mixed Agriculture", "Other land use", "Pasture", "Tree crops",
"Urban", "Water"), class = "factor"), V2 = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Emission Factor", class = "factor"),
V3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2000", "2005"), class = "factor"),
V4 = c(77.0109486116396, 69.2454348145657, 73.684103657833,
71.0430911289891, 43.136201172115, 117.358146800995, 77.4653952935238,
89.0966064142874, 71.8286578413912, 67.9099357961953, 76.7438444998728,
67.4818461466729, 50.6468079101972, 117.799797611894, 78.7347377710757,
81.3020943196897)), .Names = c("V1", "V2", "V3", "V4"), row.names = c(NA,
16L), class = "data.frame")
As you can see for the years 2000 and 2005, I have an emission factor value for each type of land use. I want to plot an histogram with the type of land use in the x axis and the emission factors in the y axis. In addition, for each land use I want the bars for the two years to be adjacents. I also want a legend showing for which years correspond the bars (either 2000 or 2005). Thanks for your help.
Here is the answer.
ggplot(EF_Lat_Am, aes(x=V1, y = V4, fill=V3, width=.85)) + geom_bar(position="dodge", stat="identity") +
labs(x = "", y = "EF (T/ha)") +
theme(axis.text=element_text(size=16),axis.title=element_text(size=20),
legend.title=element_text(size=20, face='bold'),legend.text=element_text(size=20), axis.line = element_line(colour = "black")) +
scale_fill_grey("Period") + scale_y_continuous(limits=c(0,120)) + theme_classic(base_size = 20, base_family = "")

Resources