Flipping and maintaining aspect ratio of a chart i

2019-08-10 09:15发布

问题:

This question already has an answer here:

  • ggplot2: Flip axes and maintain aspect ratio of data 1 answer

I want to create a horizontal histogram, and adjust the chart aspect ratio using ggplot2 .

For example let's say my chart is dia <- ggplot(diamonds, aes(x=color)) + geom_bar().

I can flip this to be horizontal using dia + coord_flip().

I can also adjust the aspect ratio e.g. dia + coord_fixed(ratio=.001).

But when I combine them dia + coord_flip()+ coord_fixed(ratio=.001), the chart is no longer horizontal.

Is there any way to achieve what I want using ggplot2?

回答1:

See this answer on the ggplot2 mailing list :

You can only use one coord_*() function on a given ggplot since it changes the coordinate system after everything else has been done. To change the aspect ratio, you can use the corresponding argument in the theming system:

 + coord_flip() + theme(aspect.ratio = 1)


标签: r ggplot2