Plot a 'top 10' style list/ranking in R based on numerical column of dataframe

前端 未结 1 521
一向
一向 2021-01-28 14:17

I have an R dataframe that contains a string variable and a numerical variable, and I would like to plot the top 10 strings, based on the value of the numerical variable.

相关标签:
1条回答
  • 2021-01-28 14:47

    The y-axis tick marks may be sorted alphabetically, but the points are drawn in order(from left to right) of the top10_rank dataframe. What you need to do is change the order of the y-axis. Add this to your call of ggplot + scale_y_discrete(limits=top10_rank$String) and it should work.

    ggplot(data=top10_rank, aes(x = top10_rank$Number, 
    y = top10_rank$String)) + geom_point(size=3) + scale_y_discrete(limits=top10_rank$String)
    

    Here is a link to a great resource on R graphics: R Graphics Cookbook

    0 讨论(0)
提交回复
热议问题