0

I have an input table of the following :

    city    district    shoptype    value
     A      A1           retail     1000
     A      A1        restaurant    200
     A      A2           retail    5000
     A      A2        restaurant    600
     B      A1           retail    2000
     B      A1        restaurant   3000
     B      A2            retail    400
     B      A2        restaurant    500

And I wish to plot this using barplot:

  X axis: City and District, Y axis: shoptype, size of bar: value

May I know how could I do that? I have not been able to plot what I wanted so far using 3 variables...

Pls help! thanks!

I've created an example of my desired image

Adding the dataframe code created by Akrun earlier (thanks Akrun)

df1 <- structure(list(city = c("A", "A", "A", "A", "B", "B", "B", "B"), 
   district = c("A1", "A1", "A2", "A2", "A1", "A1", "A2", "A2"), 
   shoptype = c("retail", "restaurant", "retail", "restaurant", "retail", "restaurant", "retail", "restaurant"), 
   value = c(1000L, 200L, 5000L, 600L, 2000L, 3000L, 400L, 500L)), 
  .Names = c("city", "district", "shoptype", "value"), 
  class = "data.frame", row.names = c(NA, -8L))
2
  • 1
    What do you mean by "y axis: shoptype"? What do you mean by "size of bar" – do you mean the width or the area? Can you provide an image of your expected output (using another program or literally drawing and taking a photo) so that we can have a bit of a better idea what you intend? Commented May 23, 2015 at 8:30
  • I agree with @Hugh that your description is not clear enough Commented May 23, 2015 at 8:34

2 Answers 2

2

Try something like this (providing d is your data.frame):

library(ggplot2)
ggplot(data=d,aes(x=paste(city,district),y=value,fill=shoptype)) +
  geom_bar(stat="identity",position="dodge")

or

gplot(data=d,aes(x=district,y=value,fill=shoptype)) +
  geom_bar(stat="identity",position="dodge") +
  facet_grid(. ~ city)
Sign up to request clarification or add additional context in comments.

Comments

0

Answering your question literally, I think you want width = value as one of the aesthetics.

ggplot(df1, aes(x = paste0(city, district), y = shoptype, fill = paste0(city, district), width = value)) + 
geom_bar(stat = "identity", position = "dodge")

However, I think a far more sensible option would be to use a tile:

ggplot(df1,
       aes(x = paste0(city, district), 
           y = shoptype, 
           fill = paste0(city, district), 
           width = value)) + 
  geom_bar(stat = "identity", position = "dodge")

enter image description here

3 Comments

thanks guys for all the help rendered so far, however, it isn't yet what i wanted.. I have created an image but realised i needed 10 reputations to upload an image :( basically what i wanted was close to Hugh's answer. but the x axis will consist of 1 bar for each district, and both A1 and A2 district will be further grouped by city..
i tried to edit but i couldnt upload my image... sorry to bother u guys... but how can i upload an image please.... ....
Upload an image to imgur.com and provide a link. We'll do the rest.

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.