Very simple ggplot2 histogram question
Entering edit mode
7.2 years ago
Farbod ★ 3.4k

Hi, (sorry if this question is super simple or I have asked it in a wrong place)

I have some data as bellow and I want to draw a histogram same as this with ggplot2 in R program (on linux terminal).

As you can see it is the letters from A to Z in the X axis (Function class) and the frequencies as numbers in the Y and the important point is this that each bar has its own unique color.

In addition, there is a "color help" that describe each class by the same color of each bars that I am not sure if it is some characteristics of R ggplot package or not?

I have checked some online helps but I do not know how to insert my data in the ggplo2 and asigne a unique color for each class.

Please help me and thank you in advance.


my sample numbers:

COG class.... count.... description

A ....5.......RNA processing and modification

B.... 2...... Chromatin structure and dynamics

C.... 18..... Energy production and conversion

D..... 26..... Cell cycle control, cell division, chromosome partitioning

E..... 15..... Amino acid transport and metabolism

F....... 5..... Nucleotide transport and metabolism

G...... 13.... Carbohydrate transport and metabolism

H........ 6.... Coenzyme transport and metabolism

I 15 Lipid transport and metabolism

J 20 Translation, ribosomal structure and biogenesis

K 24 Transcription

L 28 Replication, recombination and repair

M 18 Cell wall/membrane/envelope biogenesis

N 1 Cell motility

O 29 Posttranslational modification, protein turnover, chaperones

P 19 Inorganic ion transport and metabolism

Q 16 Secondary metabolites biosynthesis, transport and catabolism

R 85 General function prediction only

S 20 Function unknown

T 32 Signal transduction mechanisms

U 14 Intracellular trafficking, secretion, and vesicular transport

V 6 Defense mechanisms

Z.... 14..... Cytoskeleton

R histogram ggplot2 • 2.1k views
Entering edit mode

Hello Farbod!

It appears that your post has been cross-posted to another site:

This is typically not recommended as it runs the risk of annoying people in both communities.

Entering edit mode

Hi, I am sorry for that but I just think that I have asked my question in a wrong place

and I intended to report the answer of the topic that has answered my question first, in the second place.

Sorry again.

Entering edit mode
7.2 years ago

ggplot2 uses a dataframe as input. You can either load your data from a tab separated or comma separated file using read.table() function or manually create a dataframe by calling cbind() on the vectors of your data. If those basic data containers are a problem, I suggest you first follow some basic R tutorials which should be easy to find by googling (and will most likely cover data types early in the tutorial).

For histograms in ggplot2, this seems to be an interesting resource as well:

Entering edit mode

Dear WouterDeCoster, Hi and Thank you,

I can put my data in a text file and make R to read it from read.table() function, am i right?

it seems that the link do not work for me.

Entering edit mode

Hi I have used the script below in R studio for creating the plot from a text file but the result was awful.

Would you please have a look at it ? (I think one of the problem is in "names.arg = FunctionClass" which I have borrowed from another script) :

**table <- read.table (file='output.2.class.txt',sep=' ',header=T)

summary (table)


barplot(cog, col=c(fill=rainbow(23)), beside=T, xlab = "Function Class", ylab = "Frequency", main = "COG function classification of consensus sequences", names.arg = FunctionClass)**

and my text file is like this : (space delimited)

A 5

B 2

C 18

D 26

E 15

F 5

G 13

H 6

I 15

J 20

K 24

L 28

M 18

N 1

O 29

P 19

Q 16

R 85

S 20

T 32

U 14

V 6

Z 14


Login before adding your answer.

Traffic: 1302 users visited in the last hour
Help About
Access RSS

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6