草庐IT

关于 r:如何不成比例地拆分 y 轴以更好地在我的图中显示数据

codeneng 2023-03-28 原文

How do I split y-axis disproportionally to better show data in my plot

我有一个名为 final 的 data.frame,它看起来像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
             labels        gvs order color       f3
1            Adygei -2.3321916     1     1 353.0184
2            Basque -0.8519079     2     1 368.1515
3            French -0.9298674     3     1 365.2545
4           Italian -2.8859587     4     1 354.4481
5          Orcadian -1.4996229     5     1 350.9650
6           Russian -1.5597359     6     1 358.9736
7         Sardinian -1.4494841     7     1 355.1171
8            Tuscan -2.4279528     8     1 362.4717
9           Bedouin -3.1717421     9     2 319.3706
10            Druze -0.5058627    10     2 346.2211
11         Mozabite -2.6491331    11     2 299.5014
12      Palestinian -0.7819299    12     2 330.4576
13          Balochi -1.4095947    13     3 327.1238
14           Brahui -1.2534511    14     3 331.0927
15          Burusho  1.7958170    15     3 335.0919
16           Hazara  2.2810477    16     3 325.2444
17           Kalash -0.9258497    17     3 337.7116
18          Makrani -0.9007551    18     3 321.5726
19           Pathan  2.5543214    19     3 326.1923
20           Sindhi  2.6614486    20     3 318.7025
21            Uygur -1.2207974    21     3 322.0286
22        Cambodian  2.3706977    22     4 310.8989
23              Dai -0.9441980    23     4 305.5687
24             Daur -1.0325107    24     4 309.0984
25              Han -0.7381369    25     4 309.1198
26           Hezhen -2.7590587    26     4 296.9128
27         Japanese -0.5644325    27     4 297.9313
28             Lahu -0.8449225    28     4 307.0776
29             Miao -0.7237586    29     4 303.6593
30          Mongola -0.9452944    30     4 302.1380
31             Naxi -0.1625003    31     4 311.8019
32           Oroqen -1.2035258    32     4 308.7219
33              She -2.7758460    33     4 302.1271
34               Tu -0.7703779    34     4 307.3750
35            Tujia -1.0265275    35     4 303.5923
36             Xibo -1.1163019    36     4 295.5764
37            Yakut -3.2102686    37     4 315.0111
38               Yi -0.9614190    38     4 296.8134
39        Colombian -1.9659984    39     5 311.3134
40        Karitiana -0.9195156    40     5 300.8539
41             Maya  2.1239768    41     5 333.8995
42             Pima -3.0895998    42     5 325.3484
43            Surui -0.9377928    43     5 313.8505
44       Melanesian -1.6961014    44     6 294.5214
45           Papuan -0.7037952    45     6 286.7389
46       BantuKenya -1.9311354    46     7 152.9971
47 BantuSouthAfrica -1.8515908    47     7 133.6722
48       BiakaPygmy -1.7657017    48     7 117.5555
49         Mandenka -0.5423822    49     7 152.8525
50       MbutiPygmy -1.6244801    50     7 114.1691
51              San -0.9049735    51     7   0.0000
52           Yoruba  2.0949378    52     7 154.4460

我正在使用以下代码制作图表

1
2
3
4
5
jpeg("F3.SCZ.Jul_22.jpg", 700,700)
final$color <- as.factor(final$color)
levels(final$color) <- c("blue","yellow3","red","pink","purple","green","orange")
plot(final$gvs, final$f3, cex=2,pch = 21, bg = as.character(final$color), xaxt="n", xlab="Genetic Values", ylab="F3", main="SCZ")
dev.off()

看起来像:

我想将 y-axis 拆分为 200,使 y 值范围从 0 到 200 只占图形的 10%,而 200 到 400 则占 y 轴的 90% .这可能吗?

编辑:

这是遇到问题的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
             labels        gvs order color         f3
1            Adygei -2.3321916     1     1 0.09862109
2            Basque -0.8519079     2     1 0.09942770
3            French -0.9298674     3     1 0.10357547
4           Italian -2.8859587     4     1 0.09960179
5          Orcadian -1.4996229     5     1 0.10244666
6           Russian -1.5597359     6     1 0.10097691
7         Sardinian -1.4494841     7     1 0.10189642
8            Tuscan -2.4279528     8     1 0.09794686
9           Bedouin -3.1717421     9     2 0.09272493
10            Druze -0.5058627    10     2 0.09682272
11         Mozabite -2.6491331    11     2 0.08563901
12      Palestinian -0.7819299    12     2 0.09331649
13          Balochi -1.4095947    13     3 0.09227273
14           Brahui -1.2534511    14     3 0.09328593
15          Burusho  1.7958170    15     3 0.09396032
16           Hazara  2.2810477    16     3 0.09342432
17           Kalash -0.9258497    17     3 0.09666599
18          Makrani -0.9007551    18     3 0.09222257
19           Pathan  2.5543214    19     3 0.09468376
20           Sindhi  2.6614486    20     3 0.09172395
21            Uygur -1.2207974    21     3 0.09140727
22        Cambodian  2.3706977    22     4 0.08655821
23              Dai -0.9441980    23     4 0.08739080
24             Daur -1.0325107    24     4 0.08656669
25              Han -0.7381369    25     4 0.08764395
26           Hezhen -2.7590587    26     4 0.08802065
27         Japanese -0.5644325    27     4 0.08810874
28             Lahu -0.8449225    28     4 0.08609791
29             Miao -0.7237586    29     4 0.08700414
30          Mongola -0.9452944    30     4 0.08921706
31             Naxi -0.1625003    31     4 0.08646436
32           Oroqen -1.2035258    32     4 0.08719536
33              She -2.7758460    33     4 0.08656100
34               Tu -0.7703779    34     4 0.08818588
35            Tujia -1.0265275    35     4 0.08737680
36             Xibo -1.1163019    36     4 0.08806230
37            Yakut -3.2102686    37     4 0.08965344
38               Yi -0.9614190    38     4 0.08593454
39        Colombian -1.9659984    39     5 0.09114697
40        Karitiana -0.9195156    40     5 0.09040477
41             Maya  2.1239768    41     5 0.09068139
42             Pima -3.0895998    42     5 0.09084750
43            Surui -0.9377928    43     5 0.08925535
44       Melanesian -1.6961014    44     6 0.08430903
45           Papuan -0.7037952    45     6 0.08272786
46       BantuKenya -1.9311354    46     7 0.04668356
47 BantuSouthAfrica -1.8515908    47     7 0.03914248
48       BiakaPygmy -1.7657017    48     7 0.03546243
49         Mandenka -0.5423822    49     7 0.04612336
50       MbutiPygmy -1.6244801    50     7 0.03098719
51              San -0.9049735    51     7 0.00000000
52           Yoruba  2.0949378    52     7 0.04561542

  • 我现在做一个稍微不同的分析,虽然你的比例在我的另一张图中看起来很漂亮!


你可以这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
my_color <- as.factor(final$color)
levels(my_color) <- c("blue","yellow3","red","pink","purple","green","orange")

par(mfrow = c(1,2))

# original plot
pos <- seq(min(final$f3), max(final$f3), by = 25)  ## y-axis tick marks position.
plot(final$gvs, final$f3, cex=2, pch=21, bg = as.character(my_color),
     xaxt="n", yaxt="n", xlab="Genetic Values", ylab="F3", main="SCZ")
axis(2, at = pos, labels = pos)  ## add y-axis

# new plot
threshold <- 260  ## cut off threshold
## some rescaling
## if f3 < threshold, we take new_f3 <- 0.1 * f3
## if f3 > threshold, we take new_f3 <- f3 - 0.9 * threshold
new_f3 <- ifelse(final$f3 < threshold, 0.1 * final$f3, final$f3 - threshold * 0.9)
## we apply the same transform to `pos` to get `new_pos`
new_pos <- ifelse(pos < threshold, 0.1 * pos, pos - threshold * 0.9)

plot(final$gvs, new_f3, cex=2, pch=21, bg = as.character(my_color),
     xaxt="n", yaxt="n", xlab="Genetic Values", ylab="F3", main="SCZ")
abline(h = threshold * 0.1, lty = 3)   # threshold line
axis(2, at = new_pos, labels = pos)

  • 当我的 F3 值小于 1 时,它不再起作用。你知道为什么会这样吗?
  • 我将此图的阈值设置为 0.08,但这就是发生的情况:imgur.com/a/80Zfz
  • 我还以其他方式更改了数据,我只是为您简化,以便您获得小数。我将在编辑中重新上传数据


我会使用 scales 包中的 trans_new() 来转换 y 轴。这应该让你接近。我更喜欢连续可微的变换(第一),但你也可以在尺度上做一步改变(第二)。 H/T 致 Gregor 指出 pminpmax 处理向量并且在这里是正确的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
setwd("C:/Users/rherron1/Desktop/")
final <- read.table("Scratch2.txt", header=TRUE)
final$id <- NULL

# default y-scale
require(ggplot2)
a <- ggplot(final, aes(gvs, f3, color=factor(color)))
a <- a + geom_point()
a

# transform y-axis
require(scales)
skew <- function(x) x^2
iskew <- function(x) x^(1/2)
skew_trans <- function() trans_new("skew","skew","iskew")
b <- a + coord_trans(y="skew")
b

1
2
3
4
5
6
7
# transform y-axis
require(scales)
sku <- function(x) pmin(x, 200) + 9*pmax(x-200, 0)
isku <- function(x) pmax((x-200)/9, 0) + pmin(x, 200)
sku_trans <- function() trans_new("sku","sku","isku")
c <- a + coord_trans(y="sku")
c

  • 为什么要强制 y 轴上限为 1000?顺便说一句,您对 min 和 max 的问题是它们返回单个值,您需要返回向量。我认为您可以使用 pminpmax
  • @Gregor 哈哈!因为我的阅读理解很差!感谢您的 pmin 提示。当我回到我的办公桌时,我会解决这个问题。

有关关于 r:如何不成比例地拆分 y 轴以更好地在我的图中显示数据的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  4. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  5. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  6. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  7. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  8. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  9. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  10. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

随机推荐