Question: barplot in R
1
gravatar for saadleeshehreen
11 months ago by
saadleeshehreen60 wrote:

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 • 373 views
ADD COMMENTlink modified 11 months ago by poojasethiya80 • written 11 months ago by saadleeshehreen60
1

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 REPLYlink modified 11 months ago • written 11 months ago by cpad011211k

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 REPLYlink modified 11 months ago • written 11 months ago by WouterDeCoster38k
3
gravatar for Kevin Blighe
11 months ago by
Kevin Blighe41k
Guy's Hospital, London
Kevin Blighe41k wrote:

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 COMMENTlink written 11 months ago by Kevin Blighe41k
2
gravatar for poojasethiya
11 months ago by
poojasethiya80
poojasethiya80 wrote:

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 COMMENTlink modified 11 months ago • written 11 months ago by poojasethiya80
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1461 users visited in the last hour