barplot in R
2
1
Entering edit mode
6.5 years ago

Hi, I have a dataset like following

Drug-class  CRISPR-cas No CRISPR-cas 
Tetracyclin(3356)   46  54
Aminoglycoside(5769)    92  8
Fuscidic acid (3)   33  67
Macrolide(2238) 16  84
Oxazolidinone(3)    100 0

I ran the sample code

Drugclass <- c("Tetracyclin", "Aminoglycoside", "Fuscidic acid", "Macrolide", "Oxazolidinone")
CRISPR-cas <- c(46, 92, 33, 16, 100 )
No CRISPR-cas <- c(54, 8, 67, 84, 0)
data <- data.frame(Drugclass, CRISPR-cas, No CRISPR-Cas)

p <- plot_ly(data, x = ~Drugclass, y = ~CRISPR-Cas, type = 'bar', name = 'CRISPR-Cas') %>%
  add_trace(y = ~No CRISPR-Cas, name = 'No CRISPR-cas') %>%
  layout(yaxis = list(title = '% of genome'), barmode = 'group')

But it didn't give me a plot. How to create a barplot with this data?

R • 1.7k views
ADD COMMENT
1
Entering edit mode

You need to plot p. An object of p is created. Type in "p" after you have created p (plotly object). However, OP code will throw errors in creating data frame.

ADD REPLY
0
Entering edit mode

I added code markup to your post for increased readability. You can do this by selecting the text and clicking the 101010 button. When you compose or edit a post that button is in your toolbar, see image below:

101010 Button

But it didn't give me a plot

What happened? Did you get an error?

ADD REPLY
3
Entering edit mode
6.5 years ago

Works for me, after I modify your variable names. It is absolutely never a good idea to use spaces or hyphens in variable names. The resuting plot opens in a HTML file in a web browser:

Drugclass <- c("Tetracyclin", "Aminoglycoside", "Fuscidic acid", "Macrolide", "Oxazolidinone")
CRISPRcas <- c(46, 92, 33, 16, 100 )
NoCRISPRcas <- c(54, 8, 67, 84, 0)
data <- data.frame(Drugclass, CRISPRcas, NoCRISPRcas)

require(plotly)

plot_ly(data, x = ~Drugclass, y = ~CRISPRcas, type = 'bar', name = 'CRISPR-Cas') %>%
  add_trace(y = ~NoCRISPRcas, name = 'NoCRISPRcas') %>%
  layout(yaxis = list(title = '% of genome'), barmode = 'group')

a

In addition, If you are tired of using these 'fancy' functions that add nothing over the clever use of the base R functions, then indeed just use the base R functions: Bar Plots

Kevin

ADD COMMENT
2
Entering edit mode
6.5 years ago
poojasethiya ▴ 120

In case you are interested to plot using ggplot,

Drugclass <- c("Tetracyclin", "Aminoglycoside", "Fuscidic acid", "Macrolide", "Oxazolidinone")
CRISPRcas <- c(46, 92, 33, 16, 100 )
NoCRISPRcas <- c(54, 8, 67, 84, 0)
data <- data.frame(Drugclass, CRISPRcas, NoCRISPRcas)
data.m=melt(data,id.vars="Drugclass")

require(ggplot2)
require(reshape2)

ggplot(data.m,aes(Drugclass,value,fill=variable))+
          geom_bar(stat="identity",position="dodge")+
          xlab("DrugClass")+ylab("% of genome")+
          theme(legend.title = element_blank())

Screenshot_2018_05_28_10_06_28

ADD COMMENT

Login before adding your answer.

Traffic: 1861 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

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

Powered by the version 2.3.6