2

I simply cannot get a density plot for the following data set:

A single y Varibale and corresponding year

I have tried copying code and simply modifying and i simply can't get it to work.

I need to look like below:

Density Plot

I need the X -axis to be years and the y- axis to be in millions.

Any help would be greatly appreciated.

Code:

library(ggplot2)
library(ggthemes)
library(tidyverse)


ggplot(data = density, aes(x = welfare)) +
  geom_density(fill = 'steelblue', color = 'black') +
  theme_economist()
2
  • what is your error? Commented Apr 8, 2023 at 20:58
  • 2
    If you want to do a density plot as in your example, then your code seems to be right as it plot the probability density function of the variable welfare. so y axis shows the probability density and x is welfare. If you want to plot year vs welfare then you probably want to plot a different graph. Commented Apr 8, 2023 at 21:08

2 Answers 2

1

maybe you misunderstood the concept of a density plot - your desired output is not possible with a density plot, which can only display one variable at a time (kind of like a histogram, it shows how the observations of a single variable are distributed).

My guess is: what your are looking for is essentially a line plot (or geom_area for fancyness). Line plots are good at displaying the relationship between two variables.

library(tidyverse)

# Create reproducible example data
set.seed(100)
df <- data.frame(year = 2000:2023, wealth = runif(24))

# Plot
ggplot(df, aes(x = year, y = wealth)) +
  geom_area(alpha = 0.5, fill = "steelblue", color = "black")

example area plot

Sign up to request clarification or add additional context in comments.

1 Comment

Your probably right, i spent hours trying to make it work before i came to you all. Most likely, its not the right plot for the job. You're correct, all i want to show is that area is gained welfare. Thank you!!
0

Here is a suggestion, how we could do it:

library(tidyverse)
library(ggthemes)
library(lubridate)
library(scales)
df %>% 
  mutate(welfare_num = parse_number(welfare)) %>%
  mutate(year = ymd(paste(year, 1, 1, sep="-"))) %>% 
  ggplot(aes(x = year, y = welfare_num)) +
  geom_area(fill = "#63CB99", color = "#63CB99", alpha = 0.5) +
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
  scale_y_continuous(labels = dollar_format(prefix = "$", big.mark = ",")) +
  theme_economist()+
  labs(x = "YEAR", y="WELFARE")

data:

df <- structure(list(year = 2013:2022, welfare = c("$388,866,936.62", 
"$726,076,253.19", "$1,266,141,639.47", "$987,167,047.54", "$1,392,749,702.19", 
"$1,335,220,815.50", "$1,819,630,833.96", "$2,359,170,587.39", 
"$3,283,576,817.91", "$3,668,277,476.93")), class = "data.frame", row.names = c(NA, 
-10L))

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.