【问题标题】:Change shape, size, colour in ggplot objects更改 ggplot 对象的形状、大小、颜色
【发布时间】:2015-02-22 11:36:51
【问题描述】:

我真的很喜欢 ggplot2 包,但经常遇到所谓的基本问题,让我停了好几个小时。我有一堆来自以前模拟的 ggplot 对象,我希望更改(形状、alpha、颜色、大小)的外观,而不是重新运行模拟。 stackoverflow 上有很多类似的问题,但它们都略有不同,我还没有能够提取整体知识来轻松创建绘图。如果有人能指出我正确的方向,我很高兴。

这些图是使用与此等效的代码创建的:

ggplot(data=df) + geom_point(aes(x=Test, y=Val, ymax=max(Val), group=State, colour=State), shape=20, position=position_dodge(.5), alpha=1/2)

这是一个可重现的例子:

require(ggplot2)

# Create some data.
state <- c(TRUE,FALSE)
effect <- c(0.5, 1.0)
test <- c("A","B")

# Create combinations.
df <- expand.grid(Test=test, State=state, Effect=effect)
# Add some values.
df$Val <- rnorm(nrow(df))

# The plots were created this way.
gp <- ggplot(data=df) + geom_point(aes(x=Test, y=Val, ymax=max(Val), group=State, colour=State), shape=20, position=position_dodge(.5), alpha=1/2)
gp <- gp + facet_wrap(~ Effect)
gp <- gp + theme_bw() 
print(gp)

# Change colours and re-order legend works.
cols <- c("TRUE" = "red","FALSE" = "blue")
gp <- gp + scale_colour_manual(values = cols, breaks = c("TRUE", "FALSE")) # Change and re-order legend.
print(gp)

# Change 'shape', 'size' or 'alpha' this way does not work.
cols <- c("TRUE" = 1,"FALSE" = 3)
gp <- gp + scale_shape_manual(values=cols, breaks = c("TRUE", "FALSE"))
print(gp)
cols <- c("TRUE" = 5,"FALSE" = 10)
gp <- gp + scale_size_manual(values=cols, breaks = c("TRUE", "FALSE"))
print(gp)
cols <- c("TRUE" = 1,"FALSE" = 1/5)
gp <- gp + scale_alpha_manual(values=cols, breaks = c("TRUE", "FALSE"))
print(gp)

也许我应该以不同的方式创建情节?任何 cmets 表示赞赏。我发现如果我在“aes”中指定“形状”,我​​可以改变形状,但并非没有问题:

# Put shape inside aes.
gp <- ggplot(data=df) + geom_point(aes(x=Test, y=Val, ymax=max(Val), group=State, colour=State, shape=State), position=position_dodge(.4), alpha=1/2)
gp <- gp + facet_wrap(~ Effect)
gp <- gp + theme_bw() 
print(gp)

# Change colours and re-order legend this way works (but adds another legend).
cols <- c("TRUE" = "red","FALSE" = "blue")
#gp <- gp + scale_colour_manual(values = cols) # Changes colour.
gp <- gp + scale_colour_manual(values = cols, breaks = c("TRUE", "FALSE")) #  Changes colour, but adds a second legend when re-ordered.
print(gp)

# Try to change shape.
cols <- c("TRUE" = 1,"FALSE" = 3)
gp <- gp + scale_shape_manual(values=cols, breaks = c("TRUE", "FALSE")) # Back to one legend, but 'FALSE' does not have a symbol.
print(gp)

# Change 'size' etc. this way does not work.
cols <- c("TRUE" = 5,"FALSE" = 10)
gp <- gp + scale_size_manual(values=cols, breaks = c("TRUE", "FALSE"))
print(gp)

【问题讨论】:

  • df$Val
  • 对不起,'comb' 应该是 'df' 来生成数字。现在改变了。

标签: r ggplot2


【解决方案1】:

我简化了你的代码,更正了一点,并用形状说明了这项技术。

state <- c(TRUE,FALSE)
effect <- c(0.5, 1.0)
test <- c("A","B")

# Create combinations.
df <- expand.grid(Test=test, State=state, Effect=effect)
set.seed(1234) # needed for reproducibility
df$Val <- rnorm(nrow(df))  # changed comb to df
cols <- c("TRUE" = "red", "FALSE" = "green")
shapes <- c("TRUE" = 15, "FALSE" = 20) # added this for the shapes

ggplot(data=df) + 
  geom_point(aes(x=Test, y=Val, shape = State), size = 8) +  
  scale_shape_manual(values=shapes, breaks = c("TRUE", "FALSE"))  

编辑 这里有形状和颜色。请注意,我在上面添加了set.seed() 以实现可重复性并调整了颜色和形状:

ggplot(data=df) + 
  geom_point(aes(x=Test, y=Val, shape = State, color = State), size = 8) +  
  scale_shape_manual(values=shapes, breaks = c("TRUE", "FALSE")) +
  scale_color_manual( values= c("forestgreen", "cadetblue"), breaks = c("TRUE", "FALSE"))

【讨论】:

  • 感谢您的示例。它适用于形状,但无法添加:
  • 感谢您的示例。它适用于形状,但无法添加: + scale_colour_manual(values=cols, breaks = c("TRUE", "FALSE")) 更改颜色。如果在 aes 中指定了颜色,则它可以工作。我想只能更改在 aes 中指定的参数吗?那是我不知道的事情。另外我刚刚意识到我可以通过 ggplot(data=gp$data) 重新开始,其中 gp 是旧的绘图对象...
  • 我在第二个情节中都做了。这能回答你的问题吗?
  • 好的,谢谢!我认为这证实了需要在 geom_point(aes()) 中指定参数才能使用 scale_*_manual 进行更改?
猜你喜欢
  • 1970-01-01
  • 2022-11-25
  • 2023-03-16
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
  • 2021-06-17
  • 1970-01-01
相关资源
最近更新 更多