相关疑难解决方法(0)

在ggplot2中旋转和间隔轴标签

我有一个图,其中x轴是标签很长的因子.虽然可能不是理想的可视化,但现在我想简单地将这些标签旋转为垂直.我已经用下面的代码想出了这个部分,但正如你所看到的,标签并不完全可见.

data(diamonds)
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))
q <- qplot(cut,carat,data=diamonds,geom="boxplot")
q + opts(axis.text.x=theme_text(angle=-90))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r labels ggplot2

607
推荐指数
7
解决办法
67万
查看次数

如何在ggplot2的注释中保持对齐文本

我的例子是:

qplot(mtcars$mpg) + annotate(geom = "text", x = 30, y = 3, label = "Some text\nSome more text")
Run Code Online (Sandbox Code Playgroud)

如何让这里的文字保持对齐?所以'有些人相互排队.

r annotate text-alignment ggplot2

75
推荐指数
1
解决办法
4万
查看次数

使用ggplot2将右侧的轴标签对齐

考虑以下

d = data.frame(y=rnorm(120), 
               x=rep(c("bar", "long category name", "foo"), each=40))

ggplot(d,aes(x=x,y=y)) + 
    geom_boxplot() + 
    theme(axis.text.x=element_text(size=15, angle=90))
Run Code Online (Sandbox Code Playgroud)

标签不对齐的情节

x轴标签由标签的中心对齐.是否可以在右侧自动对齐,以便每个标签都在图表的正下方?

graphics r graph ggplot2

15
推荐指数
2
解决办法
2万
查看次数

如何在ggplot的boxplot中显示中值?

我试图通过使用ggplot()来显示框图中的中值(即水平条).R一直要求指定y轴.我有点卡住了.

p <-structure(list(TYPE = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 
3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 2L, 3L, 3L, 3L, 1L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 1L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
2L, 3L, 3L, 3L, 3L, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

11
推荐指数
1
解决办法
2万
查看次数

沿离散比例调整几何位置

我试图将文本注释放置在具有小平面和离散轴的图上。我可以使用 将注释的位置与点联系起来aes(),但我想稍微移动它们以保持点的可读性。如果微移是在数字范围内,那就没问题:

data <- data.frame(
    category = c("blue", "yellow", "red", "green"),
    rating = 1:4)

gp <- ggplot(data) + geom_point(aes(x = category, y = rating)) +
    geom_text(aes(label = "I have a label!", x = category, y = rating + 0.5))
Run Code Online (Sandbox Code Playgroud)

但如果我尝试以非数字比例(在本例中为字符)执行此操作,则会失败:

gp <- ggplot(data) + geom_point(aes(x = category, y = rating)) +
    geom_text(aes(label = "I have a label!", x = category + 0.5, y = rating))
gp

Error in unit(x, default.units) : 'x' and 'units' must have length > …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

3
推荐指数
1
解决办法
2797
查看次数

将轴标题与轴边缘完美对齐

对于ggplot2图,hjustvjust是相对于整个图定义的对齐函数。因此,当它们应用于轴标签时,它们不是相对于轴线定义的。

但是,相对于轴线调整轴标题更为自然。

具体来说,我正在寻找一种左对齐x轴标题以及上对齐和旋转y轴标题的方法。

怎样才能做到这一点呢?

对齐的轴标题

r ggplot2

2
推荐指数
1
解决办法
881
查看次数

R ggplot 多面图中的多级 x 轴标签

我尝试使用两次调用 annotate() 的技巧来获取 2 级嵌套刻度标签(此处),但我想与 facet_wrap() 结合使用

df <- tribble(
  ~action, ~gend, ~status, ~cellMean,
  "P",     "M",   "A",     1,
  "P",     "M",   "B",     2,
  "P",     "F",   "A",     1.4,
  "P",     "F",   "B",     2.6,
  "V",     "M",   "A",     2,
  "V",     "M",   "B",     3,
  "V",     "F",   "A",     2.2,
  "V",     "F",   "B",     3.8
) %>%
  mutate(action=factor(action,levels=c("P","V")),
         gend  =factor(gend,  levels=c("F","M")),
         status=factor(status,levels=c("A","B")))

df

# action obscured/overlaid
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  theme_light()
Run Code Online (Sandbox Code Playgroud)

单面板叠加

# action used as facet
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 facet-wrap multi-level x-axis

1
推荐指数
1
解决办法
731
查看次数