Question: Issue with barplot ggplot2
0
gravatar for Nitin
4.2 years ago by
Nitin140
Germany
Nitin140 wrote:

I have following data

d1 d2

a -9.278

b -5.582

c -5.266

d -5.01

e -3.833

I wrote R code to plot barplot as follows

library(ggplot2)

dat<-read.csv("input.csv",sep=",")

dat1<-dat[,-3]

ggplot(dat11,aes(x = d1, y = d2)) +

geom_bar(fill="#e34a33",width=0.34,stat="identity") +

scale_x_discrete(limits=dat1$d1) +

coord_flip() +

theme_bw() + theme(legend.position = "none", panel.grid.major=element_blank(),

                 panel.grid.minor=element_blank(),legend.key = 

element_blank(),axis.title.x = element_text(size=15),axis.text.x =

element_text(size=16), axis.title.y =

element_text(size=15),axis.text.y=element_text(size=17),

panel.border = element_rect(colour = "black",size=0.7))

This code gave the plot as follows

But this plot has problem bars are on right side but it should be towards left and x axis should start 0 to -10. Can any body to suggest soultions for this problem

Thanks, Sa

R • 1.2k views
ADD COMMENTlink modified 4.2 years ago by vivekbhr540 • written 4.2 years ago by Nitin140
0
gravatar for dariober
4.2 years ago by
dariober10k
WCIP | Glasgow | UK
dariober10k wrote:

What about this? (NB: Usually these questions are better suited to StackOverflow or R mailing list)

library(ggplot2)

dat1<- data.frame(d1= c(-9, -5, -4, -3), d2= c('a', 'b', 'c', 'd'))

ggplot(data= dat1, aes(x = d2, y = -d1)) +
    geom_bar(fill="#e34a33", width=0.34,stat="identity") +
    coord_flip() +
    scale_y_discrete(breaks= seq(0, 10), labels= seq(0, -10)) +
    theme_bw() + theme(legend.position = "none",
        panel.grid.major=element_blank(),
        panel.grid.minor= element_blank(),legend.key = element_blank(),
        axis.title.x = element_text(size=15),
        axis.text.x = element_text(size=16),
        axis.title.y = element_text(size=15),
        axis.text.y=element_text(size=17),
        panel.border = element_rect(colour = "black",size=0.7))
ADD COMMENTlink written 4.2 years ago by dariober10k
0
gravatar for vivekbhr
4.2 years ago by
vivekbhr540
Germany
vivekbhr540 wrote:

Hi

Plot absolute values to change plot orientation.. : aes(x = d1, y = abs(d2))

use scale_y_continuous(limits=c(-10,0)) to get the scale up to 10.

ADD COMMENTlink modified 4.2 years ago • written 4.2 years ago by vivekbhr540
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: 764 users visited in the last hour