barplot in R
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?
• 2,191 views
•
link
2 answers
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')
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
• 1 views
•
link
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())
• 1 views
•
link
Log in to answer this question.


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:
What happened? Did you get an error?
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.