0
votes

My data is a single column like this:

Number Assigned Row 
1        1
2        1
3        2
4        1
5        2
6        3
...      ...

When I plot using barplot I get what I want:

barplot

However when I use ggplot + geom_bar I get this:

ggplot + geom_bar

This is my code for ggplot:

  count <- data.frame(alldata[[xaxis]])
  ggplot(data=count, aes(x="My X Axis", y="My Y Axis")) +
  geom_bar(stat="identity")

versus the code I use for barplot:

  counts <- table(alldata[[xaxis]])
  barplot(counts,
          main = xaxis,
          xlab = "Percentile",
          cex.names = 0.8,
          col=c("darkblue","red"), beside = group != "NA")
1
remove the comments around your x and y variables. make sure the x and y variables entered match columns in your dataframe - morgan121
@RAB what do you mean by "remove the comments"? - lopawag
you dont need th " around your x and y variable names. I meant quotes - my bad - morgan121
Isn't aes(x, y) just the labels for the plot? How would I plot vs the count then? Since Y should be the count of cases. - lopawag
nope, thats xlab and ylab. aes(x ,y) are the names of the variables you would like to show on the x and y column - morgan121

1 Answers

0
votes

Say this is your data:

df <- data.frame(AssRow = sample(1:3, 100, T, c(0.2, 0.5, 0.3)))
head(df)
#   AssRow
#1      2
#2      1
#3      2
#4      3
#5      2
#6      2

This will get you a bar chart of the count of each Assigned Row, and colour them:

ggplot(df, aes(x=AssRow, fill=as.factor(AssRow))) + 
  geom_bar()

To change the labels use xlab ylab/make the background prettier:

ggplot(df, aes(x=AssRow, fill=as.factor(AssRow))) + 
  geom_bar() +
  xlab("My X-label") +
  ylab("My Y label") +
  theme_bw()

Output:

enter image description here