簡體   English   中英

R中ggplot2的對數縮放

[英]Logarithmic scaling with ggplot2 in R

我正在嘗試使用ggplot2創建圖。 有幾個非常小的值要顯示,還有一些更大的值。 我想使用對數縮放以適當的方式顯示所有這些。 這是我的工作:

plotPointsPre <- ggplot(data = solverEntries, aes(x = val, y = instance, 
                                                  color = solver, group = solver))

...

finalPlot <- plotPointsPre + coord_trans(x = 'log10') + geom_point() +
xlab("costs") + ylab("instance")

結果如下:

這個

它與沒有coord_trans(x = 'log10')

但是,如果我將其與y軸一起使用:

有用

如何在x軸上實現對數縮放? 此外,它與x軸無關,如果我切換x和y的值,則它在x軸上起作用,而不再在y軸上起作用。 因此,顯示的值似乎有些問題。 有人知道如何解決此問題嗎?

編輯-這是solverEntries使用的數據:

solverEntries <- data.frame(instance = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20),
                 solver = c(4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1),
                 time = c(1, 24, 13, 6, 1, 41, 15, 5, 1, 26, 16, 5, 1, 39, 7, 4, 1, 28, 11, 3, 1, 31, 12, 3, 1, 38, 20, 3, 1, 37, 10, 4, 1, 25, 11, 3, 1, 32, 18, 4, 1, 27, 21, 3, 1, 23, 22, 3, 1, 30, 17, 2, 1, 36, 8, 3, 1, 37, 19, 4, 1, 40, 21, 3, 1, 29, 11, 4, 1, 33, 10, 3, 1, 34, 9, 3, 1, 35, 14, 3),
                 val = c(6553.48, 6565.6, 6565.6, 6577.72, 6568.04, 7117.14, 6578.98, 6609.28, 6559.54, 6561.98, 6561.98, 6592.28, 6547.42, 7537.64, 6549.86, 6555.92, 6546.24, 6557.18, 6557.18, 6589.92, 6586.22, 6588.66, 6588.66, 6631.08, 6547.42, 7172.86, 6569.3, 6582.6, 6547.42, 6583.78, 6547.42, 6575.28, 6555.92, 6565.68, 6565.68, 6575.36, 6551.04, 6551.04, 6551.04, 6563.16, 6549.86, 6549.86, 6549.86, 6555.92, 6544.98, 6549.86, 6549.86, 6561.98, 6558.36, 6563.24, 6563.24, 6578.98, 6566.86, 7080.78, 6570.48, 6572.92, 6565.6, 7073.46, 6580.16, 6612.9, 6557.18, 7351.04, 6562.06, 6593.54, 6547.42, 6552.3, 6552.3, 6558.36, 6553.48, 6576.54, 6576.54, 6612.9, 6555.92, 6560.8, 6560.8, 6570.48, 6566.86, 6617.78, 6572.92, 6578.98))

您當前表單數據無法登錄分配-最val 6500和更高的約10%。 如果要拉伸數據,則可以使用scales::trans_new()進行自定義轉換,或者這是一個更簡單的版本,僅減去基線值即可使對數轉換有用。 減去6500后,較小的值將映射到大約50,較大的值將映射到1000,這是對數刻度的更合適范圍。 然后,我們對中斷應用相同的變換,以便標簽將顯示在正確的位置。 (即標簽6550映射到映射到6550-6500 = 50的數據)

如果您想使基礎值更加可區分,則此方法會有所幫助,但要以使值之間的基礎比例失真為代價。 您可能可以通過選擇有用的中斷並用縮放統計信息標記它們來幫助解決此問題,例如

7000 +7% over min

my_breaks <- c(6550, 6600, 6750, 7000, 7500)
baseline = 6500

library(ggplot2)
ggplot(data = solverEntries, 
       aes(x = val - baseline, y = instance, 
           color = solver, group = solver)) +
  geom_point() +
  scale_x_log10(breaks = my_breaks - baseline,
                labels = my_breaks, name = "val")

在此處輸入圖片說明

這是您要找的東西嗎?

x_data <- seq(from=1,to=50)
y_data <- 2*x_data+rnorm(n=50,mean=0,sd=5)

#non log y
ggplot()+
  aes(x=x_data,y=y_data)+
  geom_point()

#log y scale
ggplot()+
  aes(x=x_data,y=y_data)+
  geom_point()+
  scale_y_log10()

#log x scale
ggplot()+
  aes(x=x_data,y=y_data)+
  geom_point()+
  scale_x_log10()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM