0% found this document useful (0 votes)
8 views4 pages

R Script For Updated R Code

This R script performs ANOVA and Tukey HSD tests on a dataset to analyze the variable 'pjbuah' across different 'varietas'. It summarizes the data, generates a bar plot with error bars, and ensures that the bars start at zero on the y-axis while setting specific limits and breaks. The final plot is customized with themes and labels for better visualization.

Uploaded by

ruslimuhammad110
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

R Script For Updated R Code

This R script performs ANOVA and Tukey HSD tests on a dataset to analyze the variable 'pjbuah' across different 'varietas'. It summarizes the data, generates a bar plot with error bars, and ensures that the bars start at zero on the y-axis while setting specific limits and breaks. The final plot is customized with themes and labels for better visualization.

Uploaded by

ruslimuhammad110
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R Script for Updated R Code — Bars Touch x-Axis (Start at Zero)

# Load libraries

library(ggplot2)

library(dplyr)

library(multcompView)

# ANOVA and Tukey HSD test

anova_result <- aov(pjbuah ~ varietas, data = dat)

tukey_result <- TukeyHSD(anova_result)

# Significance letters

letters_df <- multcompLetters4(anova_result, tukey_result)

letters_table <- [Link](letters_df$varietas)

letters_table$varietas <- rownames(letters_table)

colnames(letters_table)[1] <- "Letters"

# Summary data

summary_df <- dat %>%

group_by(varietas) %>%

summarise(mean_pjbuah = mean(pjbuah),

se = sd(pjbuah) / sqrt(n())) %>%

left_join(letters_table, by = "varietas")

# Bar plot

p <- ggplot(summary_df, aes(x = varietas, y = mean_pjbuah)) +

geom_bar(stat = "identity", fill = "#E7B800", color = "black", width = 0.5) +


geom_errorbar(aes(ymin = mean_pjbuah - se, ymax = mean_pjbuah + se), width = 0.2)
+

geom_text(aes(y = mean_pjbuah + se + 1.0, label = Letters), size = 5) +

labs(x = "Varietas", y = "Panjang Buah (cm)") +

scale_y_continuous(

expand = c(0, 0), # Remove padding so bars start at y=0

limits = c(0, max(summary_df$mean_pjbuah + summary_df$se) + 1)

)+

theme_minimal(base_size = 14) +

theme(

[Link] = element_line(color = "black", size = 0.8),

[Link] = element_text(color = "black"),

[Link] = element_text(color = "black"),

[Link] = element_line(color = "black"),

[Link] = element_blank(),

[Link] = element_blank()

# Show the plot

print(p)

scale_y_continuous(expand = c(0, 0), limits = ...)


Final Updated Code (with y-axis limit and breaks)

# Load libraries

library(ggplot2)

library(dplyr)

library(multcompView)

# ANOVA and Tukey HSD test

anova_result <- aov(pjbuah ~ varietas, data = dat)

tukey_result <- TukeyHSD(anova_result)

# Significance letters

letters_df <- multcompLetters4(anova_result, tukey_result)

letters_table <- [Link](letters_df$varietas)

letters_table$varietas <- rownames(letters_table)

colnames(letters_table)[1] <- "Letters"

# Summary data

summary_df <- dat %>%

group_by(varietas) %>%

summarise(mean_pjbuah = mean(pjbuah),

se = sd(pjbuah) / sqrt(n())) %>%

left_join(letters_table, by = "varietas")

# Bar plot with y-axis from 0 to 20 and breaks every 5 units

p <- ggplot(summary_df, aes(x = varietas, y = mean_pjbuah)) +

geom_bar(stat = "identity", fill = "#E7B800", color = "black", width = 0.5) +

geom_errorbar(aes(ymin = mean_pjbuah - se, ymax = mean_pjbuah + se), width = 0.2)


+
geom_text(aes(y = mean_pjbuah + se + 0.5, label = Letters), size = 5) +

labs(x = "Varietas", y = "Panjang Buah (cm)") +

scale_y_continuous(

limits = c(0, 20), # Fixed range

breaks = seq(0, 20, 5), # Tick marks: 0, 5, 10, 15, 20

expand = c(0, 0) # Bars touch baseline

)+

theme_minimal(base_size = 14) +

theme(

[Link] = element_line(color = "black", size = 0.8),

[Link] = element_text(color = "black"),

[Link] = element_text(color = "black"),

[Link] = element_line(color = "black"),

[Link] = element_blank(),

[Link] = element_blank()

# Show plot

print(p)

You might also like