更改ggplot对象中的形状,大小和颜色

时间:2015-02-22 11:36:51

标签: r ggplot2

我非常喜欢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)

也许我应该以不同方式创建情节?任何评论都表示赞赏。我发现如果我在'aes'中指定'shape',我可以改变形状,但不是没有问题:

# 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)

1 个答案:

答案 0 :(得分: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"))  

enter image description here

修改 这是形状和颜色。请注意,我在上面添加了set.seed()以获得可重复性和调整cols和形状:

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"))

enter image description here