4

I need help with this please. I have searched here but not got the right output.

I am trying to plot this in R, so I can plot 3 files side-by-side in a single plot using GGplot. The output I desire (plotted with excel) is this

desired histogram

What i am getting using GGplot is this

ggplot output

The R code i am using is this

A1 <- read.table("A1.txt", header = T, sep = "\t")
library(ggplot2)
ggplot(A1, aes(x = count)) + geom_bar()

The data is a tab-delimited file like this

length  count
26  344776
27  289439
18  673395
28  338146
19  710702
20  928326
21  3491352
22  2724981
23  699007
24  726121
25  472509

The length, as it were will only be labels on the x axis for the counts plotted on the y-axis.

2
  • You don't need a histogram. You need a bar plot. Try a bar plot with length as x axis and count as y axis Commented Jun 12, 2018 at 10:43
  • Thanks a lot @Rohit. barplot works too. Commented Jun 12, 2018 at 13:24

1 Answer 1

5

Is this what you wanted?

ggplot(A1, aes(x = as.character(length), y=count)) + geom_bar(stat="identity")

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

6 Comments

Thanks a lot. This works great. How can I make the labels on the x-axis correspond to each bar...like 18 19 20 21 etc. What i have now is 17.5 20.0 22.5 25.0 27.5
(stat="identity") appears to be the main difference to the code I wrote earlier. what is the meaning of this please?
if you dont use stat it would calculate the counts automatically. when you say stat="identity" you are basically saying 'dont count number of times you see 18 in lengths but take the count column as that value instead'
the labels are dependent on your length column. it should show exactly the same values. if you want integers you can use round
also you should mark this as answer if this was what you are looking for
|

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.