-3

newbie in R: wanted to to ask how I can plot two numerical variables (say smoking% and liquor consumption%) across 50 US states - I can easily do that in excel but still struggling in R; any ideas?enter image description here

#Let's say here is my code:
smoking <- c(21, 23.4, 83)
drinking <- c(19.5, 28.9, 57)
States <- c(CA, NH, NJ)

How can i plot as barplot for smoking (red) and drinking (black) with states on x axis. Sorry Stacko won't allow me to upload pic or data file. Thanks again!

enter image description here

SamV
  • 118
  • 1
  • 7
  • 2
    Can you share some sample data or code? It would likely be possibly with ggplot. – emendez Jun 17 '20 at 20:49
  • Have a look at [this](https://stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes) and [this](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales). – kmacierzanka Jun 18 '20 at 09:56
  • Is there a way to do this in R? Yes... what kind of plot were you looking to create? Bar chart, scatter chart, something combinig the two?... there are lots of options that will be avaialble, but what are you trying to convey with the plot? Go from there, then think about what plot would best show that result, then finally when you know what you want - go for the coding. – chemdork123 Jun 18 '20 at 15:26
  • Thanks Edward, KJM and chemdork. I wanted to create a barchart. I am adding the data and the excel snapshot in the original post above. I have used R for stats and plotting. And just can't fathom that R won't be able to do this. – SamV Jun 18 '20 at 22:24
  • this comes close: https://i.stack.imgur.com/ybr4U.jpg But not quite there. I just need one Y axis scale since both my variable smoking and drinking are a percent. And the X axis is a categorical with names of states. – SamV Jun 18 '20 at 22:41
  • You should post some code showing what you have tried. – william3031 Jun 18 '20 at 22:50
  • @william3031 I tried posting the code here - too long it says - don't get me wrong, however I am not asking you to help me based on your judgement after I justify I deserve so; Thanks anyway!! – SamV Jun 19 '20 at 15:53
  • It is more about actually showing that you have tried to solve the problem before asking for help, while also making it easier for others to help you with your question. This might help you with the answer: http://www.sthda.com/english/wiki/ggplot2-barplots-quick-start-guide-r-software-and-data-visualization and this might help you with getting an answer here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – william3031 Jun 20 '20 at 06:18
  • @william3031 many thanks; this looks too complicated - it should not be so - I have used ggplot for my plots and statistical analysis using R; but this plot can be done in excel in 5 seconds - I can't give R more than 2 hours on this - doesn't seem a practical approach – SamV Jun 20 '20 at 15:11
  • It probably isn't complicated. But you're not providing the information that will help others help you with what you want to do despite many requests for it. – william3031 Jun 21 '20 at 20:54

1 Answers1

0

The data you provided, when it is put in a table, is not 'tidy'. You can read more about tidy data here.

Here is what I have done to get your desired output, assuming you start with data like df:

smoking <- c(21, 23.4, 83)
drinking <- c(19.5, 28.9, 57)
States <- c("CA", "NH", "NJ")

# the 'messy' data
df <- data.frame("States" = States,
                 "Smoking" = smoking,
                 "Drinking" = drinking)
> df
  States Smoking Drinking
1     CA    21.0     19.5
2     NH    23.4     28.9
3     NJ    83.0     57.0

When can then use the tidyr package to transform it into long format. This is much easier to work with in ggplot2.

library(tidyr)
df_tidy <- pivot_longer(df, -States, names_to = "Type")
> df_tidy
# A tibble: 6 x 3
  States Type     value
  <chr>  <chr>    <dbl>
1 CA     Smoking   21  
2 CA     Drinking  19.5
3 NH     Smoking   23.4
4 NH     Drinking  28.9
5 NJ     Smoking   83  
6 NJ     Drinking  57 

Finally, we can plot this using ggplot2:

library(ggplot2)
ggplot(df_tidy, aes(x = States, y = value)) +
        geom_bar(aes(fill = Type), position = "dodge", stat = "identity")

We can also just use geom_col() instead of geom_bar(), which uses stat = "identity" by default, as we find out from this tidyverse help page.

ggplot(df_tidy, aes(x = States, y = value)) +
        geom_col(aes(fill = Type), position = "dodge")

In either case, we get:

enter image description here

kmacierzanka
  • 747
  • 4
  • 17
  • I am still studying tibble and df conversion - i did that long time ago in roger peng's data analysis (JHopkins) class when i had no grey hair :-) – SamV Jun 25 '20 at 13:29
  • is there a way to plot this as a 3D on a map of that state? I am actually intending to plot a 3d bar/elevator plot on a map of States in India. Any ideas? @stefan? – SamV Jul 06 '20 at 07:27
  • I want to appreciate the help provided by all above users in this thread and the stackoverflow community for your assistance in visualization of data in one of my upcoming publications. Kindly contact me if you do not consent to using your user ID in the acknowledgements. Thank you and all the best! Stay safe and healthy!! – SamV Oct 21 '20 at 13:27