brtk
brtk

Reputation: 117

How to leave only the column in gglot without blank spaces?

I created chart like this:

enter image description here

I would like to delete white blank spaces, and leave only the column.

enter image description here

My code:

 ggplot(melt(livechat_data())[c(8,9),],aes(x='',
                                            y=as.integer(value)
                                            ,fill=factor(variable, levels=c("nie","tak" )))) +
          geom_col(position='stack',width = .3) +
          coord_flip()+
          theme_minimal()+
          labs(x = "", y = "konwersja%",fill="Wysłano płatne zapytanie") +
          scale_y_continuous(labels = comma)+
          theme( plot.title=element_text(size=15,face="bold"),
                 axis.text=element_text(size=15),
                 axis.title=element_text(size=15,face="bold"),
                 text=element_text(size=15),
                 legend.position = 'bottom')+
          scale_fill_manual(values = c("tak" = '#00cc00',
                                       "nie" = '#737373'))

Upvotes: 1

Views: 95

Answers (1)

chemdork123
chemdork123

Reputation: 13843

Like some others have said, you can take your scale_y_continuous line and add expand=c(0,0) to remove the space around the y axis. In your case, that won't work because your data is flipped: you need to use scale_x_continuous. I show you an example below with dummy data to better and more clearly illustrate this point:

df <- data.frame(x=c(0,0), y=c(20, 80), sam=c('This', 'That'))
p <- ggplot(df, aes(x=x,y=y)) + geom_col(aes(fill=sam)) +coord_flip()
p

enter image description here

Coord_flip() + scale_x_continuous(expand=c(0,0)) removes TOP and BOTTOM spaces

p + scale_x_continuous(expand=c(0,0))

enter image description here

Coord_flip() + scale_y_continuous(expand=c(0,0)) removes LEFT and RIGHT spaces

p + scale_y_continuous(expand=c(0,0))

enter image description here

Put 'em together, and you get this:

p + scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))

enter image description here

No more border or space. So although you mentioned you tried scale_y_continuous(expand=c(0,0)) you should be able to tell from above that this won't work to remove the "upper" and "lower" spaces in your graph: it will only remove spaces on the left and right side. Like the example I posted, your plot is flipped with coord_flip(), so you need scale_x_continuous.

Upvotes: 1

Related Questions