title: “Creating nice bar graphs” author: “Unius” date: “2023-06-18” output: html_document —library(ggplot2) library(dplyr) Data_Summary_2022 Data_Summary_2022 Data_Summary_2022 Data_Summary_2022
Data_Summary_2022 <- Data_Summary_2022 %>% group_by(pop, irr)
%>% summarise(mean_yield = mean(Grain yield (kg/ha)), se
= sd(Grain yield (kg/ha)) / sqrt(n()))
ggplot(Data_Summary_2022, aes(x = pop, y = mean_yield, fill = as.factor(irr))) + geom_bar(stat = “identity”, position = “dodge”, width = 0.5) + geom_errorbar(aes(ymin = mean_yield - se, ymax = mean_yield + se), width = 0.2, position = position_dodge(width = 0.5)) + labs(x = “Planting rate (plants/ac)”, y = “Grain yield (kg/ha)”, fill = “Irrigation strategies”) + theme_minimal() + theme(legend.position = “top”, legend.justification = “right”, plot.background = element_rect(fill = “white”), panel.grid.major.y = element_line(color = “gray80”), plot.margin = margin(10, 50, 10, 10, “pt”), plot.title = element_text(size = 12, face = “bold”), axis.title = element_text(size = 11,face = “bold”), legend.title = element_text(size = 11, face = “bold”), legend.text = element_text(size = 11))
Data_Summary_2022 <- Data_Summary_2022 %>% group_by(irr, nrate)
%>% summarise(mean_yield = mean(Grain yield (kg/ha)), se
= sd(Grain yield (kg/ha)) / sqrt(n()))
ggplot(Data_Summary_2022, aes(x = irr, y = mean_yield, fill = as.factor(nrate))) + geom_bar(stat = “identity”, position = position_dodge(width = 0.8), width = 0.5) + geom_errorbar(aes(ymin = mean_yield - se, ymax = mean_yield + se), width = 0.2, position = position_dodge(width = 0.8)) + labs(x = “Irrigation strategies”, y = “Grain yield (kg/ha)”, fill = “N-rates (lbs/ac)”) + theme_minimal() + theme(legend.position = “top”, legend.justification = “right”, plot.background = element_rect(fill = “ivory”), panel.grid.major.y = element_line(color = “gray80”), plot.margin = margin(10, 50, 10, 10, “pt”), plot.title = element_text(size = 12, face = “bold”), axis.title = element_text(size = 11, face = “bold”), legend.title = element_text(size = 11,face = “bold”), legend.text = element_text(size = 10,face = “bold”))
Data_Summary_2022 <- Data_Summary_2022 %>% group_by(pop, nrate)
%>% summarise(mean_yield = mean(Grain yield (kg/ha)), se
= sd(Grain yield (kg/ha)) / sqrt(n()))
ggplot(Data_Summary_2022, aes(x = pop, y = mean_yield, fill = as.factor(nrate))) + geom_bar(stat = “identity”, position = position_dodge(width = 0.8), width = 0.5) + geom_errorbar(aes(ymin = mean_yield - se, ymax = mean_yield + se), width = 0.2, position = position_dodge(width = 0.8)) + labs(x = “Planting rate (plants/ac)”, y = “Grain yield (kg/ha)”, fill = “N-rates (lbs/ac):”) + theme_minimal() + theme(legend.position = “top”, legend.justification = “right”, plot.background = element_rect(fill = “ivory”), panel.grid.major.y = element_line(color = “gray80”), plot.margin = margin(10, 50, 10, 10, “pt”), plot.title = element_text(size = 12, face = “bold”), axis.title = element_text(size = 11, face = “bold”), axis.text.x = element_text(size = 9, color = “black”, face = “bold”), axis.text.y = element_text(size = 9, color = “black”, face = “bold”), legend.title = element_text(size = 11, face = “bold”), legend.text = element_text(size = 10, face = “bold”))
Data_Summary_2022
ggplot(Data_Summary_2022, aes(x = irr, y = Grain Yield,
fill = as.factor(nrate))) + geom_bar(stat = “identity”, position =
“dodge”, width = 0.5) + labs(x = “Planting rate strategies”, y = “Grain
Yield (bu/ac)”, fill = “N-rate strategies”) + theme_minimal() +
theme(legend.position = “top”, legend.justification = “right”,
plot.background = element_rect(fill = “white”), panel.grid.major.y =
element_line(color = “gray80”), plot.margin = margin(10, 30, 10, 10,
“pt”), # Adjust the margin of the plot (reduced width) plot.title =
element_text(size = 15, face = “bold”), axis.title = element_text(size =
11, face = “bold”), legend.title = element_text(size = 13, face =
“bold”), legend.text = element_text(size = 11))
Data_Summary_2022 ggplot(Data_Summary_2022, aes(x = irr, y =
Grain yield (kg/ha), fill = as.factor(nrate))) +
geom_bar(stat = “identity”, position = “dodge”, width = 0.5) + labs(x =
“Planting rate strategies”, y = “Grain Yield (bu/ac)”, fill = “N-rate
strategies”) + theme_minimal() + theme(legend.position = “top”,
legend.justification = “right”, plot.background = element_rect(fill =
“white”), panel.grid.major.y = element_line(color = “gray80”),
plot.margin = margin(10, 30, 10, 10, “pt”), # Adjust the margin of the
plot (reduced width) plot.title = element_text(size = 15, face =
“bold”), axis.title = element_text(size = 11), axis.title.x =
element_text(face = “bold”), axis.title.y = element_text(face = “bold”),
legend.title = element_text(size = 13, face = “bold”), legend.text =
element_text(size = 11))
ggplot(Data_Summary_2022, aes(x = irr, y = Grain Yield,
fill = as.factor(nrate))) + geom_bar(stat = “identity”, position =
“dodge”, width = 0.5) + labs(x = “Planting rate strategies”, y = “Grain
Yield (bu/ac)”, fill = “N-rate strategies”) + theme_minimal() +
theme(legend.position = “top”, legend.justification = “right”,
plot.background = element_rect(fill = “snow1”), # Change the background
color to snow1 panel.grid.major.y = element_line(color = “gray80”),
plot.margin = margin(10, 30, 10, 10, “pt”), # Adjust the margin of the
plot (reduced width) plot.title = element_text(size = 15, face =
“bold”), axis.title = element_text(size = 11, face = “bold”),
legend.title = element_text(size = 13, face = “bold”), legend.text =
element_text(size = 11))
#Data1 library(ggplot2)
data1 <- data.frame( “Irrigation strategies” = c(“36" with soil sensor”, “36" drip + fertigation”, “36" drip”, “72" drip + fertigation”, “72" drip”, “Non-irrigated”), “Grain yield (kg/ha)” = c(5406, 5128, 4943, 4795, 4641, 2470), “std” = c(701.7, 837.1, 644.0, 889.4, 969.0, 465.7), “groups” = c(“a”, “ab”, “bc”, “bc”, “c”, “d”) ) data1 # Plot the bar graph with error bars ggplot(data1, aes(x = reorder(Irrigation.strategies, -Grain.yield..kg.ha.), y = Grain.yield..kg.ha., fill = groups)) + geom_bar(stat = “identity”, position = “dodge”) + geom_errorbar(aes(ymin = Grain.yield..kg.ha. - std, ymax = Grain.yield..kg.ha. + std), width = 0.2, position = position_dodge(0.9)) + labs(x = “Irrigation strategies”, y = “Grain yield (kg/ha)”, fill = “Groups”) + theme_minimal()
ggplot(Data_Summary_2022, aes(x = irr, y = Grain Yield,
fill = as.factor(nrate))) + geom_bar(stat = “identity”, position =
“dodge”, width = 0.5) + labs(x = “Plant Population”, y = “Grain Yield
(bu/ac)”, fill = “Nitrogen Rate (lbs/ac)”) + theme_minimal() +
theme(legend.position = “top”, legend.justification = “right”,
plot.background = element_rect(fill = “white”), panel.grid.major.y =
element_line(color = “gray80”), plot.margin = margin(10, 30, 10, 10,
“pt”), plot.title = element_text(size = 16, face = “bold”), axis.title =
element_text(size = 13), legend.title = element_text(size = 13, face =
“bold”), legend.text = element_text(size = 11), axis.text.x =
element_text(angle = 45, hjust = 1)) # Angle the x-axis label by 45
degrees
library(ggplot2)
library(ggplot2)
data <- data.frame( “N_rate_strategies” = c(120, 180, 240, 300), “Grain_Yield” = c(156, 182, 188, 193), “std” = c(33.7, 43.0, 49.9, 56.6), “Mean_group” = c(“c”, “b”, “ab”, “a”) )
plot <- ggplot(data, aes(x = factor(N_rate_strategies), y = Grain_Yield, fill = Mean_group)) + geom_col(width = 0.5) + geom_errorbar(aes(ymin = Grain_Yield - std, ymax = Grain_Yield + std), width = 0.2, color = “black”) + geom_text(aes(label = Grain_Yield), vjust = -0.5, color = “black”, size = 3.5, position = position_stack(vjust = 0.5)) + labs(x = “N-rate strategies”, y = “Grain Yield (bu/ac)”, fill = “Mean group”) + scale_fill_manual(values = c(“a” = “blue”, “b” = “red”, “ab” = “green”, “c” = “purple”)) + theme_minimal()
plot
library(ggplot2)
data <- data.frame( “N_rate_strategies” = c(120, 180, 240, 300), “Grain_Yield” = c(156, 182, 188, 193), “std” = c(33.7, 43.0, 49.9, 56.6), “Mean_group” = c(“c”, “b”, “ab”, “a”) )
data4 plot <- ggplot(data1, aes(x = Irrigation.strategies, y = Grain.yield..kg.ha., fill = groups)) + geom_col(width = 0.5) + geom_errorbar(aes(ymin = Grain.yield..kg.ha. - std, ymax = Grain.yield..kg.ha. + std), width = 0.2, color = “black”) + geom_text(aes(label = Grain.yield..kg.ha.), vjust = -0.5, color = “black”, size = 4, position = position_stack(vjust = 0.5)) + labs(x = “Planting rate strategies”, y = “Grain yield (bu/ac)”, fill = “Mean group”) + scale_fill_manual(values = c(“a” = “blue”, “ab” = “red”, “bc” = “green”, “c” = “purple”, “d” = “yellow”)) + theme_minimal() + theme(legend.position = “top”, legend.justification = “center”, legend.box = “horizontal”)
ggsave(“bar_graph.png”, plot, width = 8, height = 6, units = “in”)
plot
plot <- ggplot(data1, aes(x = Irrigation.strategies, y = Grain.yield..kg.ha., fill = groups)) + geom_col(width = 0.5) + geom_errorbar(aes(ymin = Grain.yield..kg.ha. - std, ymax = Grain.yield..kg.ha. + std), width = 0.2, color = “black”) + geom_text(aes(label = Grain.yield..kg.ha.), vjust = -0.5, color = “black”, size = 4, position = position_stack(vjust = 0.5)) + labs(x = “Irrigation strategies”, y = “Grain yield (kg/ha)”, fill = “Mean group:”) + scale_fill_manual(values = c(“a” = “blue”, “ab” = “red”, “bc” = “green”, “c” = “purple”, “d” = “yellow”)) + theme_minimal() + theme(legend.position = “top”, legend.justification = “center”, legend.box = “horizontal”, axis.text.x = element_text(angle = 45, hjust = 1))
ggsave(“bar_graph.png”, plot, width = 8, height = 6, units = “in”)
plot
plot <- ggplot(data1, aes(x = Irrigation.strategies, y = Grain.yield..kg.ha., fill = groups)) + geom_col(width = 0.5) + geom_errorbar(aes(ymin = Grain.yield..kg.ha. - std, ymax = Grain.yield..kg.ha. + std), width = 0.2, color = “black”) + geom_text(aes(label = Grain.yield..kg.ha.), vjust = -0.5, color = “black”, size = 3, position = position_stack(vjust = 0.5)) + labs(x = “Irrigation strategies”, y = “Grain yield (kg/ha)”, fill = “Mean group:”) + scale_fill_manual(values = c(“a” = “blue”, “ab” = “red”, “bc” = “green”, “c” = “purple”, “d” = “yellow”)) + theme_minimal() + theme(legend.position = “top”, legend.justification = “center”, legend.box = “horizontal”, axis.text.x = element_text(angle = 45, hjust = 1), axis.text = element_text(face = “bold”), axis.title = element_text(face = “bold”), legend.text = element_text(face = “bold”), legend.title = element_text(face = “bold”), plot.title = element_text(face = “bold”))
ggsave(“bar_graph.png”, plot, width = 8, height = 6, units = “in”)
plot
plot <- ggplot(data3, aes(x = Nitrogen_rate, y = Grain_yield, fill = groups)) + geom_bar(stat = “identity”, width = 15) + geom_errorbar(aes(ymin = Grain_yield - std, ymax = Grain_yield + std), width = 2, color = “black”) + labs(x = “Nitrogen rate (lbs/ac)”, y = “Grain yield (kg/ha)”, fill = “Groups”) + geom_text(aes(label = Grain_yield), vjust = -0.5, color = “black”, size = 2, position = position_stack(vjust = 0.5)) + scale_fill_manual(values = c(“a” = “blue”, “ab” = “red”, “bc” = “green”, “c” = “pink”, “d” = “yellow”)) + theme_minimal() + theme(legend.position = “top”, legend.justification = “center”, legend.box = “horizontal”, axis.text.x = element_text(angle = 45, hjust = 1), axis.text = element_text(face = “bold”), axis.title = element_text(face = “bold”), legend.text = element_text(face = “bold”), legend.title = element_text(face = “bold”), plot.title = element_text(face = “bold”), plot.background = element_rect(fill = “darkgray”)) # Change the background color to dark gray
data <- data.frame( Nitrogen_rate = c(300, 240, 180, 120), Grain_yield = c(4898, 4783, 4624, 3951), std = c(856.0761, 1092.7857, 1268.3072, 1437.1534), groups = c(“a”, “ab”, “b”, “c”) )
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.