0

My dataset has this form:

name = c("h", "s", "i", "s", "o","R")
value = c(37, 27, 20, 20, 5, 5)
df = data.frame(name, value)

I tried every idea that I could, but I think that I am missing some basic knowledge about how to solve it. adding stats = "count"

 ggplot(df,aes(name) ) +
     geom_bar(stat = "count", show.legend = TRUE) 

I am obtaing or: - A plot with all the columns - Error messages

My expected solutions is: A barplot using ggplot, with 4 columns as "h", "s", "i & s", "o & R"

And in y-axis, each of the columns with the next values: 37, 27, 20, 5

2 Answers 2

3

After summarising the data as suggested by @caldwellst, you could plot the bar plot using

library(dplyr)
library(ggplot2)

df %>%
  group_by(value) %>%
  summarize(name = toString(name)) %>%
  ggplot() + aes(name, value, label = value, fill = name) +
  geom_col() + geom_text(nudge_y = 1)

enter image description here

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

Comments

3

You need to adjust your dataset prior to plotting, as ggplot has no idea that you want to visualize identical values as a single bar. Using dplyr you could simply do:

library(dplyr)

df %>%
  group_by(value) %>%
  summarize(name = paste(name, collapse = " & "))

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.