Issue with barplot ggplot2
2
0
Entering edit mode
9.0 years ago
Nitin ▴ 170

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 • 2.3k views
ADD COMMENT
0
Entering edit mode
9.0 years ago

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 COMMENT
0
Entering edit mode
9.0 years ago
vivekbhr ▴ 700

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 COMMENT

Login before adding your answer.

Traffic: 2361 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