2

I want to create a density plot with the following data:

 interval       fr      mi      ab
0x              9765    3631    12985
1x              2125    2656    601
2x              1299    2493    191
3x              493     2234    78
4x              141     1559    20
5x and more     75      1325    23

On the X-Axis I want to have the Intervals and on the Y-Axis I want to have the density of "fr", "mi" and "ab" in different colors.

My imagination was something like this graph.

enter image description here

My problem is that I don't know how to get the density on the Y-Axis. I tried it with geom_density, but it didn't work. The best result I accomplished was using the following code:

DS29 <-as.data.frame(DS29)
  DS29$interval <- factor(DS29$interval, levels = DS29$interval)
  DS29 <- melt (DS29,id=c("interval"))
output$DS51<- renderPlot({
  plot_tab6 <- ggplot(DS29, aes(x= interval,y = value, fill=variable, group = variable)) +
    geom_col()+
    geom_line()
  return(plot_tab6)
})

This gives me the following plot, which is not the result I want to have. Do you have an idea how I could get to my wanted result? Thank you very much.

enter image description here

1
  • this? library(tidyverse); read.table(text=" interval fr mi ab 0x 9765 3631 12985 1x 2125 2656 601 2x 1299 2493 191 3x 493 2234 78 4x 141 1559 20", header=T) %>% gather(key, value, -interval) %>% ggplot(aes(x=value, fill=key)) + geom_density() Commented Dec 20, 2017 at 11:50

1 Answer 1

1

Seeing your sample data, I am not sure if you want to use geom_density. If you type ?geom_density, you will see some example codes. If I take one example from the help page, you may see things that you are missing.

ggplot(diamonds, aes(depth, fill = cut, colour = cut)) +
geom_density(alpha = 0.1) +
xlim(55, 70)

For x-axis, depth is a continuous variable, not a categorical variable. Your current data has a categorical variable in x-axis. For geom_density, you are looking for density of something at a value on x-axis. The example code above shows that the density of diamonds classified as "Ideal" has high density around 61.5-62, suggesting that the largest proportion "Ideal" diamonds have depth value around 61.5-62. Indeed, mean value for depth of "Ideal" diamond is 61.71. This means that you need multiple data points to calculate density. Your data has only one data point for each interval for each group (e.g., ab, fr, mi). So, I do not think your data is not ready for calculating density.

If you want to draw a graphic similar to what you suggested in your question using the current data, I think you need to 1) convert interval to a numeric variable, 2) transform the data into long format, and 3) use stat_smooth.

library(tidyverse)

mydf %>%
mutate(interval = as.numeric(sub(x = as.character(interval), pattern = "x", replacement = ""))) %>%
gather(key = group, value = value, - interval) -> temp

ggplot(temp, aes(x = interval, y = value, fill = group)) +
stat_smooth(geom = "area", span = 0.4, method = "loess", alpha = 0.4)

enter image description here

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

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.