ggplot2问题-如何在对数刻度上绘制值<0的图形

时间:2019-01-07 10:32:09

标签: r ggplot2 shinydashboard

我一直在玩一个闪亮的应用程序,以可视化来自国际货币基金组织数据库的数据。

应用程序中的一个标签包括使用geom_point()将两个指示符相对于另一个指示符。

我认为以对数比例绘制数据可能有助于可视化,因为数据分布经常偏斜。

一方面,对于严格为正的值,不会遇到任何问题。 另一方面,由于某些指标以百分比变化衡量,可能为负,因此ggplot只是从图中排除了这些值(考虑到负值的对数未定义的事实,这是有道理的)。

听起来像是一个奇怪的问题,但这是:

有没有一种方法可以显示负值,同时仍以对数刻度绘制轴?

请查看以下代码:

output$interact1 <- renderPlot({

x <- ggplot(filter(testplot, Year == input$y), 
            aes_string(x = input$indicator, y = input$indicator2))    

x + geom_jitter(aes_string( col = "Continent", size = "Population")) +

  scale_size_continuous(breaks = c(0,5,50,100,200,1000)) + 
  theme(legend.position = "bottom") + 
  scale_x_log10() + 
  scale_y_log10() +
  geom_smooth(aes_string(input$indicator, input$indicator2), 
              method = "loess", col = "black") +
  geom_smooth(method = "lm", col = "red", show.legend = F) + 
  ylab(gsub("_"," ",input$indicator2)) + 
  xlab(gsub("_", " ", input$indicator)) +
  labs(caption = paste(gsub("_"," ",input$indicator2), 
                       "vs.", 
                       gsub("_", " ", input$indicator)))})

下面的屏幕快照也可能有助于理解我在这里试图做什么:

GDP per capita vs GDP % Change

编辑:

为回答以下问题,所附的条形图显示了2018年GDP百分比变化的分布(我知道标签可能有点难以理解)。Bar plot of GDP % Change in 2018

0 个答案:

没有答案