1

I am using a dataset wherein the values are as follows :

ad          fl
300         1
400         1
450         2
600         4
700         2
350         1
700         3
900         5
1100        5
700         6
430         3
640         4

I want to plot a bar chart based on the median values of ad against the fl. Note that I need the median values of ad based on fl. I am using this command

ggplot(data=dataForGraph, aes(x=fl, y= median(ad))) +
  geom_bar(stat="identity", fill="steelblue")

But it doesn't give a correct graph. I feel that it is because of the median.

Based on the answer by @d.b I used the following R Code. :

ggplot(dataForGraph, aes(x = factor(NUM_FLOORS), y = AV_BLDG)) + geom_bar(stat = "summary", fun.y = "median") + scale_y_continuous(labels=fancy_scientific)

I got this graph

enter image description here

Now I want to add color gradient (light blue too dark) dark for large value and light for low. Can anybody help me in the same?

Thanks

markus
  • 25,843
  • 5
  • 39
  • 58
dper
  • 884
  • 1
  • 8
  • 31
  • 5
    `ggplot(df1, aes(x = factor(fl), y = ad)) + geom_bar(stat = "summary", fun.y = "median")` – d.b Mar 18 '19 at 21:21
  • Hello @d.b Thank you so much for your response, However I wanted to change the axis on the graph as well. Right now it's showing 0e+00 and so on. I want the axis to be of same unit as ad. Can you please suggest a way to do that ? – dper Mar 18 '19 at 21:28
  • 1
    Consider whether bars are appropriate here. The medians are based on a small number of observations, between 1 and 3. It may be more appropriate to show each data point, with the median overlaid as a line or coloured point. – neilfws Mar 18 '19 at 21:38
  • @neilfws I just added a dummy data here.. the original dataset has 60k entries and it works. I want to ad a gradient color as well to the graph. trying to use `hp+scale_fill_gradient(low="blue", high="red") hp <- ggplot(dataForGraph, aes(x = factor(fl), y = ad)) + geom_bar(stat = "summary", fun.y = "median") + scale_y_continuous(labels=fancy_scientific)` But the gradient is not working. – dper Mar 18 '19 at 22:01
  • Please don't change your question, instead ask a new one if you don't find the answer in some other post. This might be helpful: https://stackoverflow.com/questions/37343114/fill-gradient-color-not-working-with-geom-bar-of-ggplot2 – markus Mar 18 '19 at 22:36
  • @markus Thank you for providing the link. However, I am not able to add the color gradient in the same. I am using the following command : `ggplot(dataForGraph, aes(x = factor(fl), y = ad)) + scale_fill_gradient(low = "green", high = "red") + geom_bar(stat = "summary", fun.y = "median") + scale_y_continuous(labels=fancy_scientific)` Can anyone tell why is the gradient not working? – dper Mar 18 '19 at 22:54

0 Answers0