How to create a grouped tablet with R

I am trying to create a grouped graph with R. I tried the following code to create a simple barcode.

x=c(99,9,104,67,86,53,83,29,127,31,179,86,74,80,100,150,68,18,81,47) names(x)= c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R","T","W","V","Y") barplot(x) y= c(105673,18140,92426,76776,93974,53470,75155,30700,77847,28863,124602,55703, 50160,60685,78693,69581,70846,18285,92789,45728) names(y)= c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R","T","W","V","Y") barplot(y) 

I need to combine the above two bars. I can’t figure out how to combine them.

I tried with gplot.

 require(ggplot2) data(mydata) head(mydata) ggplot(mydata, aes(aminoacid, fill=cut)) + geom_bar(position="dodge") + opts(title="aminoacid analysis ") Error in data.frame(x = c(2L, 3L, 5L, 4L, 7L, 6L, 9L, 8L, 10L, 12L, 11L, : arguments imply differing number of rows: 21, 228 

I also tried the following code.

 counts <- table(mydata) barplot(counts, main="amino acid analysis",`xlab="aminoacid codes", col=c("darkblue","red")`legend = rownames(counts), beside=TRUE)) Error in barplot.default(counts, main = "aminoacid analysis", : 'height' must be a vector or a matrix 

How can I solve these errors?

Please help me create a grouped barplot with R.

+4
source share
1 answer

Welcome to SO.

You can watch ggplot2 , on the Hadley page you will find detailed examples of how to do this. Here is an example:

 # if you haven't installed ggplot, if yes leave this line out install.packages("ggplot2") # choose your favorite mirror require(ggplot2) data(diamonds) # check the dataset head(diamonds) # plot it ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") + opts(title="Examplary Grouped Barplot") 

enter image description here

What's nice about the ggplot2 package is that you can easily change the visualization of a parameter (aesthetic, simple). For example, you can look into facects or grouped charts instead of grouping. It is also well documented on the Hadley page.

For completeness, here is also an example ggplot2 @quickR

 # Grouped Bar Plot counts <- table(mtcars$vs, mtcars$gear) barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue","red"), legend = rownames(counts), beside=TRUE) 

enter image description here

+15
source

Source: https://habr.com/ru/post/1381445/


All Articles