ggplot翻转和变换坐标 [英] ggplot flip and transform coordinate

查看:234
本文介绍了ggplot翻转和变换坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换坐标(而不是比例/值),以便y变量(翻转到x)间隔更好。我认为它会是 coord_trans(y =log10) coord_trans(y =log2) ,但这似乎不起作用。

  library(ggplot2)
library(scales)

dat < - structure(列表(中位数= c(2893,0,907.5,1315,0,84,98,953,
0,0,1349,17.5,48.5,7,28,18,14,37.5,0,383,
86.5,816.5,38,41,38,1302,14,0,1304,754,424.5,0,35.5,
28,32,0,39),
名称= c(名称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,
name 35,name 36,name 37,name 38,name 39)),
.Names = c(median,name),row.names = c (NA,-39L),class =data.frame)


dat = transform(dat,name = reorder(name,median))

ggplot(dat)+
geom_point(aes(y = median,x = name))+
#coord_trans(y =log10)+
coord_flip()+
theme_bw ()+
主题(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_line(color ='black'),
panel.background = element_blank(),
axis.title.y = element_blank())

我不想重新调整值:

  geom_point(aes(y = median,x = name))+ 
scale_y_log10()+
coord_flip()+
theme_bw +
主题(
panel.grid.major = element_blank(),
panel.grid.minor = element _blank(),
panel.border = element_blank(),
axis.line = element_line(color ='black'),
panel.background = element_blank(),
axis .title.y = element_blank())


解决方案

现在我明白你的问题了,这里的答案是希望它能帮助别人。



首先, coord _ * 函数不能堆叠 - 它们互相覆盖。因此,您使用它的方式, coord_flip(...)覆盖 coord_trans(...)看到任何转换。

要获得基于线性间距的刻度标记和带有翻转坐标的log10间距,可以使用 breaks = ... 参数为 scale _ * ,如下所示:

  ggplot(dat)+ 
geom_point(aes(y = median,x = name))+
scale_y_log10(breaks = 1000 *(1:3))+#note使用中断= ...
coord_flip()+
theme_bw()+
主题(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_line(color ='black'),
panel.background = element_blank(),
axis .title.y = element_blank())


I want to transform the coordinate (not the scale/values) so that the y variable (flipped to x) is better spaced. I thought it would be coord_trans(y="log10") or coord_trans(y="log2") or something, but this does not seem to work.

library(ggplot2)
library(scales)

dat <- structure(list(median=c(2893, 0, 907.5, 1315, 0, 84, 98, 953, 
                               0, 0, 1349, 17.5, 48.5, 7, 28, 18, 14, 37.5, 0, 383, 220.5, 49, 
                               86.5, 816.5, 38, 41, 38, 1302, 14, 0, 1304, 754, 424.5, 0, 35.5, 
                               28, 32, 0, 39), 
                      name = c("name 1", "name 2", "name 3", "name 4", 
                               "name 5", "name 6", "name 7", "name 8", "name 9", "name 10", 
                               "name 11", "name 12", "name 13", "name 14", "name 15", "name 16", 
                               "name 17", "name 18", "name 19", "name 20", "name 21", "name 22", 
                               "name 23", "name 24", "name 25", "name 26", "name 27", "name 28", 
                               "name 29", "name 30", "name 31", "name 32", "name 33", "name 34", 
                               "name 35", "name 36", "name 37", "name 38", "name 39")), 
                 .Names = c("median", "name"), row.names = c(NA, -39L), class = "data.frame")


dat = transform(dat, name = reorder(name, median))

ggplot(dat) +
  geom_point(aes(y=median, x=name)) +
  #coord_trans(y="log10") +
  coord_flip() +
  theme_bw() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.line = element_line(color = 'black'),
    panel.background = element_blank(),
    axis.title.y = element_blank())

I do NOT want to rescale the values:

ggplot(dat) +
  geom_point(aes(y=median, x=name)) +
  scale_y_log10() +
  coord_flip() +
  theme_bw() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.line = element_line(color = 'black'),
    panel.background = element_blank(),
    axis.title.y = element_blank())

解决方案

OK so now that I understand your question, here's the answer in hopes it will help others.

First, the coord_* functions cannot be stacked - they override each other. So tthe way you are using it, coord_flip(...) overrides coord_trans(...) and you don't see any transformations.

To get log10 spacing with tick labels based on the linear spacing, with flipped coordinates, you can use the breaks=... argument to scale_*, as in:

ggplot(dat) +
  geom_point(aes(y=median, x=name)) +
  scale_y_log10(breaks=1000*(1:3)) +   # note use of breaks=...
  coord_flip() +
  theme_bw() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.line = element_line(color = 'black'),
    panel.background = element_blank(),
    axis.title.y = element_blank())

这篇关于ggplot翻转和变换坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆