0

I am totally new to R and I need a Histogram which shows 5 different years on the x axis and the corresponding median on the y axis. And I am getting the error stat_bin() can only have an x or y aesthetic. I don´t know how to solve this :(

library(ggplot2)

myvars <- c("1965", "1975", "1985", "1995", "2005")
newdata <- Countries_GDP_1960_2020[myvars]
sapply(newdata, mean, na.rm=TRUE)
median1 <- sapply(newdata, median, na.rm=TRUE)

myvars1 <- as.numeric(as.character(myvars))

median2 <- data.frame(median1)


p <- ggplot(median2, aes(x=myvars1, y=median1)) + geom_bar(stat='identity') +
  geom_histogram()

p
Simon42
  • 3
  • 1
  • 3
  • 3
    A histogram by itself is only of one variable, not two. You can choose to stack, dodge, or facet based on another variable, but the basic statistic should be on one only. However, if you provide some logic for grouping such as `fill=` then it can stack them. See [`?geom_histogram`](https://ggplot2.tidyverse.org/reference/geom_histogram.html) for some examples. Beyond that, you will need to add sample data to make this question reproducible so that can better see what you're working with. See https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans May 22 '22 at 17:22
  • 2
    It looks like you want a bar or column graph, not a histogram. A histogram is essentially for one continuous variable (although, it can be manipulated for other purposes). If you want to use an x and y, use `geom_col()` (instead of `geom_bar` and `geom_histogram`). If that's not what you're looking for, try making your question reproducible. It looks like you're fairly new to SO; welcome to the community! Include sample data, like the output from `dput(head(dataObject)))` and what type of output you are expecting. Check it out: [making R questions reproducible](https://stackoverflow.com/q/596 – Kat May 22 '22 at 17:23

1 Answers1

0

You can plot the median using geom_col(stat = "summary", fun = "median"), like this:

library(ggplot2)

# Create example data
df <- data.frame(
  year = sample(c("1965", "1975", "1985", "1995", "2005"), 100, replace = TRUE),
  value = runif(100, 1, 1000))

# View example data
head(df)
#>   year    value
#> 1 1965 230.6510
#> 2 1975 868.3858
#> 3 1975 356.4177
#> 4 1985 762.8586
#> 5 1985 581.7105
#> 6 1965 274.9215

# Plot bar plot where bar heigh corresponds to median of each year
ggplot(df, aes(x = year, y = value)) +
  geom_bar(stat = "summary", fun = "median") +
  ylab("median value")

enter image description here

Skaqqs
  • 4,010
  • 1
  • 7
  • 21