I would like to create a bar graph using data in long format.
Here is my code:
library(data.table)
library(dplyr)
library(ggplot2)
dv1 = runif(n = 100, min = 1, max = 7)
dv2 = runif(n = 100, min = 1, max = 7)
dv3 = runif(n = 100, min = 1, max = 7)
country <- rep(c("India", "US", "Poland"), length.out = 100)
df <- data.frame(country, dv1, dv2, dv3)
df$casenum <- seq.int(nrow(df))
df2 <- df %>% select(casenum, country, dv1, dv2, dv3)
df.melt <- data.table::melt(setDT(df2), id = 1L,
measure = list(c(3,4,5)),
value.name = c("dv"))
df.melt2 <- df2 %>%
select(casenum, country)
df.melt.final <- dplyr::left_join(df.melt, df.melt2, by="casenum")
ggplot(df.melt.final, aes(fill=variable, y=dv, x=country)) +
geom_bar(position="dodge", stat="identity")
The bar graph looks like this, but the means on the graph do not correspond with the actual means in the data. What can I do with it?


