0

I am in a bit of a struggle and I can't find a solution (it should be very simple) my Code is this

df Ch1 V1 V2 ID A a1 a2 1 B b1 b2 2 C a1 b2 1 D d1 d2 3 ... 

in total we have values ranging from 1 to 9. I simply want to plot how often 1(,2,3,...,9) occurs in this data frame. My code is this

ggplot(df,aes(ID))+ #because I read that leaving y value makes ggplot count the occurences which is T geom_bar()+ 

This works but unfortunately I get this as a result enter image description here I want all values to be displayed though. I tried to modify this with scale_x_continuous but it didn't work (made the whole x-axis go away and display only 1)

I know I can also create a table = table(df)

But I want to find a universal solution. Because later I want to be able to apply this while making several bars per x-axis value with dependency on V1 or V2 ...

Thank you very much for your help!

5
  • 3
    Convert ID to a factor then plot. Commented Jan 24, 2020 at 0:50
  • Perfect worked - easy as that. I actually did factor(ID). Commented Jan 24, 2020 at 0:54
  • If I would like to plot V1 with different bars depending on how often ID-value occurs for V1-value, how exactly would I do that? Commented Jan 24, 2020 at 1:09
  • I am trying this ggplot(data=df%>% gather(Variable, ID, -V1), aes(x = factor(reorder(V1,-ID)), fill = Variable)) Commented Jan 24, 2020 at 1:17
  • Just add fill = V1 to the aesthetic. You shouldn't need to reshape your data. ggplot(df, aes(x = ID, fill = V1)) + geom_bar() The bars are stacked by default - if you don't want this use geom_bar(position = "dodge"). Commented Jan 24, 2020 at 1:26

1 Answer 1

1

According to the OP, the intention is to create

several bars per x-axis value with dependency on V1 or V2

This can be solved either by using fill = V1 and position = "dodge" as already suggested H 1 or by facetting. Both approaches have their merits depending on the aspect the OP wants to focus on.

Note that in all variants ID is turned into a discrete variable (using factor()) and by overriding the default axis title to solve the issue with labeling the x-axis.

Dogded position

library(ggplot2) ggplot(df) + aes(x = factor(ID), fill = V1) + geom_bar(position = "dodge") + xlab("ID") 

enter image description here

This is good if the focus is on comparing the differences between V1 within each ID value.

Facets

library(ggplot2) ggplot(df) + aes(x = factor(ID), fill = V1) + geom_bar() + xlab("ID") + facet_wrap(~ V1, nrow = 1L) 

enter image description here

Here, the focus is on comparing the distribution of ID counts within each V1.

Colouring the bars in addition to faceting is redundant (but I find it aesthetically more pleasing as compared to all-black bars).

Data

As there were no reproducible data supplied in the question, I have tried to simulate the data by

nr <- 1000L set.seed(123L) # required to reproduce the data df <- data.frame(Ch1 = sample(LETTERS[1:4], nr, TRUE), V1 = paste0(sample(letters[1:4], nr, TRUE), "1"), V2 = paste0(sample(letters[1:4], nr, TRUE), "2"), ID = pmin(1L + rgeom(nr, 0.3), 9L) ) 

"Raw" plot for comparison with OP's chart

library(ggplot2) ggplot(df) + aes(x = ID) + geom_bar() 

enter image description here

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.