【问题标题】:In R , how to draw a scatter plot by ggplot2 with different scale but equal length在 R 中,如何通过 ggplot2 绘制不同比例但长度相等的散点图
【发布时间】:2020-09-24 08:38:23
【问题描述】:

我的数据在下面。

X <- c(0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.3, 1, 2, 3) 
Y <- c(0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.3, 1, 2, 3)

我想通过 R 包 ggplot2 绘制 X 与 Y 的散点图。 X 轴应为 0.01, 0.02, 0.03, 0.04, ..., 0.09, 0.1, 0.2, 0.3, 0.4, ..., 0.9, 1, 2, 3, 4, 5。Y 轴也应为 0.01 , 0.02, 0.03, 0.04, ..., 0.09, 0.1, 0.2, 0.3, 0.4, ..., 0.9, 1, 2, 3, 4, 5. 我的问题是我怎样才能使任意两个之间的长度X轴(也是Y轴)上的相邻点相同吗?例如,0.01 到 0.02 之间的长度与 X 轴上 0.1 到 0.2 之间的长度相同。

【问题讨论】:

  • 我猜你必须创建自己的转换(参见here)并将其交给scale_x/y_continuous(trans = "your transformation")
  • 嘿!你检查过 R Graphics Cookbook 的Chapter 8 吗?可通过 Bookdown 免费在线获取。第 8 章可能包含您正在寻找的内容。

标签: r ggplot2


【解决方案1】:
d <- tibble(X= c(0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.3, 1, 2, 3) ,
            Y = c(0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.3, 1, 2, 3))

d %>% ggplot(aes(x=X, y=Y)) +
       geom_point() +
        scale_x_log10() +
        scale_y_log10()

这是你想要的吗?

【讨论】:

  • 感谢您的回复,我想添加 0.01, 0.02, 0.03, ..., 0.09, 0.1, 0.2, ...,1, 2, 3, 4, 5两个轴。现在该图在两个轴上都只有 0.01、0.1、1。
  • scale_x_log10 的在线文档中查看breaks=
  • 非常感谢。我试过那个命令。它接近我想要的情节。
【解决方案2】:

这是一个更详尽的解决方案,您可以将数据分成小箱,按每个箱创建分面图,然后将分面重新粘贴在一起。

X <- c(0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.3, 1, 2, 3) 
Y <- c(0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.3, 1, 2, 3)

library( data.table )
library( ggplot2 )
plotdata <- data.table( x = X, y = Y )

#create bins to plot
limits <- data.table( from = c(0,0.01,0.1,1,10) )
limits[, to := shift( from, type="lead", fill = Inf ) ]
limits[, bin_name := paste(from, to, sep = "-") ]
#     from    to bin_name
# 1:  0.00  0.01   0-0.01
# 2:  0.01  0.10 0.01-0.1
# 3:  0.10  1.00    0.1-1
# 4:  1.00 10.00     1-10
# 5: 10.00   Inf   10-Inf

#bin plotdata for x and y values by joining
plotdata[ limits, bin_x := i.bin_name, on = .(x >= from, x < to ) ][]
plotdata[ limits, bin_y := i.bin_name, on = .(y >= from, y < to ) ][]

#get factorder right for ordering facets
plotdata[, bin_x.f := forcats::fct_rev( factor( bin_x ) ) ]

#almost what we want, but scales are not right
ggplot( data = plotdata ) +
  geom_point( aes(x = x, y = y ) ) +
  facet_grid( bin_x.f ~ bin_y )

#get facetscales-package from github
## devtools::install_github("zeehio/facetscales")  # <-- run once !
library(facetscales)

#build y scales
scales_y <- list(
  "0.01-0.1" = scale_y_continuous(expand = c(0,0), limits = c(0,0.1), breaks = seq(0,0.1,0.01), labels = c( seq(0,0.09,0.01), "" ) ),
  "0.1-1"    = scale_y_continuous(expand = c(0,0), limits = c(0.1,1), breaks = seq(0.1,1,0.1), labels = c( seq(0.1,0.9,0.1), "" ) ),
  "1-10"     = scale_y_continuous(expand = c(0,0), limits = c(1,10), breaks = seq(1,10,1), labels = c( seq(1,9,1), "" ) )
)
#build x scales
scales_x <- list(
  "0.01-0.1" = scale_x_continuous(expand = c(0,0), limits = c(0,0.1), breaks = seq(0,0.1,0.01), labels = c( seq(0,0.09,0.01), "" ) ),
  "0.1-1"    = scale_x_continuous(expand = c(0,0), limits = c(0.1,1), breaks = seq(0.1,1,0.1), labels = c( seq(0.1,0.9,0.1), "" ) ),
  "1-10"     = scale_x_continuous(expand = c(0,0), limits = c(1,10), breaks = seq(1,10,1), labels = c( seq(1,9,1), "" ) )
)

#now we get
ggplot( data = plotdata ) +
  geom_point( aes(x = x, y = y ) ) +
  facet_grid_sc( rows = vars(bin_x.f), cols = vars(bin_y), scales = list( x = scales_x, y = scales_y ) )+
  theme(
    #remove the facet descriptions completely
    strip.background = element_blank(),
    strip.text = element_blank(),
    #drop space bewteen panels
    panel.spacing = unit(0, "lines"),
    #rotate x_axis labels
    axis.text.x = element_text( angle = 90, hjust = 1, vjust = 0.5 ) 
  )

如您所见,这是可行的,但是在设置 x 和 y 轴时,由于 expand = c(0,0) 的原因,恰好位于 bin 的最小值/最大值的点会被截断。

【讨论】:

    【解决方案3】:

    对数变换可以是您正在查看的接近解决方案,但它不提供相同的距离:

    df <- data.frame(X=X,Y=Y)
    p = ggplot(df,aes(x=X,y=Y))+
      geom_line() + 
      geom_point(size=4) 
    
    p+
      scale_x_continuous(trans = "log10") +
      scale_y_continuous(trans = "log10") 
    

    请查看以下网站:来自 MASS 的 Animals 数据也是同样的问题: http://www.sthda.com/english/wiki/ggplot2-axis-scales-and-transformations

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      相关资源
      最近更新 更多