Reputation: 1
I have 2 factors on X-axis rose and crapemyrtle and the space between those bars toomuch, i am trying to reduce that space but not working. Could anyone help me out of this? This is the code.
choice01 <- read.csv("Research_Data/choice-rose-cm-1-lysathia-2024.csv")
choice01
library(agricolae)
library(lme4)
library(lmerTest)
library(car)
model01 <- lmer(damage ~ host + (1 | rep), data = choice01)
Anova(model01)
ranova(model01)
# Quality Checking residuals
residuals01 <- residuals(model01)
plot(residuals01)
hist(residuals01)
qqnorm(residuals01)
qqline(residuals01)
# Looking at significance between the means
library(emmeans)
library(multcomp)
mymeans <- emmeans(model01, specs = "host")
cld(mymeans)
library(dplyr) # For easier data manipulation
# Calculate Means and SE by host
summary_data01 <- choice01 %>%
group_by(host) %>%
summarize(
mean_damage = mean(damage, na.rm = TRUE), # na.rm = TRUE handles missing data
sd_damage = sd(damage, na.rm = TRUE),
n = n(), # Number of observations for SE calculation
se_damage = sd_damage / sqrt(n)
)
# The most reliable way to control the order of categories on a discrete axis in ggplot2 is to convert the variable (Host) to a factor and then set the desired order of the factor levels.
summary_data01 # Check the results
# Plugging the summary data
summary_data01$host <- factor(summary_data01$host, levels = c("Rose", "Crapemyrtle"))
# Now, use the summary_data you just created in your ggplot code. This is the crucial step you were missing:
# install.packages("stringr")
library(ggplot2)
library(stringr)
ggplot(summary_data01, aes(x = host, y = mean_damage, fill = host)) +
geom_bar(stat = "identity", color = "white", width = 0.16) +
# First width makes the bars wider, potentially filling the space entirely (or leaving very small gaps).
# second width make bars narrower
geom_errorbar(aes(ymin = mean_damage - se_damage, ymax = mean_damage + se_damage),
width = 0.04
) + # Error bars
labs(
title = "Choice 01",
x = "Host plants",
y = expression(atop("No. " ~ italic("L. ludoviciana"), "feeding damage scores per leaf"))
) + # Italicized species name
theme_minimal() +
# Making x and y-axis lines, tick marks, and labels pitch black
theme(
panel.grid.major = element_blank(), # Remove all major grid lines
panel.grid.minor = element_blank(), # Remove all minor grid lines
axis.text.x = element_text(angle = 0, hjust = 0.5, color = "black", size = 12), # Black x-axis text
axis.text.y = element_text(color = "black", size = 12), # Black y-axis text
axis.title.x = element_text(color = "black", size = 12, margin = margin(t = 10)), # Black x-axis title # Move x-axis title down
axis.title.y = element_text(color = "black", size = 12, margin = margin(r = 10)), # Black y-axis title # Move y-axis title left
axis.line = element_line(color = "black", size = 0.5), # Black axis lines
axis.ticks = element_line(color = "black", size = 0.5), # Black tick marks
) +
scale_x_discrete(breaks = c(levels(summary_data01$host)), expand = c(0.09, 0.09)) +
# 0.09 (first value): This adds 9% padding to the left side of the x-axis.
# 0.09 (second value): This adds 9% padding to the right side of the x-axis.
scale_y_continuous(
limits = c(0, 5), # Extends y-axis to 5
breaks = seq(0, 5, by = 1.00), # Ensures a major tick at 5
expand = c(0, 0)
) # Remove padding
I want to reduce space between the two bars without changing the width of the bars and there should be little space between y-axis and first bar!
Upvotes: -1
Views: 45