我正在创建一条包含两行的ggplot,每行分别来自不同的几何。例如:
df = data.frame(
x.v = seq(0, 1, 0.025),
y.v = runif(41)
)
straight.line = data.frame(
Inter = c(0),
Slope = c(1)
)
p = ggplot() +
geom_point(
mapping = aes(
x = x.v,
y = y.v
),
data = df,
colour = "blue"
) +
geom_smooth(
mapping = aes(
x = x.v,
y = y.v,
colour = "line of best fit"
),
data = df,
method = "lm",
show.legend = NA
) +
geom_abline(
mapping = aes(
intercept = Inter,
slope = Slope,
colour = "y = x"
),
data = straight.line,
show.legend = NA
) +
guides(
fill = "none",
linetype = "none",
shape = "none",
size = "none"
)
这给出了输出:
如您所见,图例中有奇怪的对角线。 answer to a similar question表示可以使用show.legend = NA
来解决此问题。但是,正如您在上面的代码中看到的那样,我做到了,并且它没有改变结果。
有人知道在图例中添加对角线的原因吗,我还能如何解决?谢谢。
编辑:是否与this重复的问题。这可能是答案,但是当链接中的答案使用填充,而我使用颜色时,该如何应用呢?
如果我尝试
+ guides(colour = guide_legend(override.aes = list(colour = NULL)))
我收到错误
Error in check.length("col") : 'gpar' element 'col' must not be length 0
如果我尝试
+ guides(colour = guide_legend(override.aes = listfill = NULL)))
我得到了错误
Error in `$<-.data.frame`(`*tmp*`, "fill", value = character(0)) :
replacement has 0 rows, data has 1
答案 0 :(得分:3)
以下作品:
d = {'Data':[['hello', 'she', 'can'],
['not', 'no', 'more', 'to']]}
df = pd.DataFrame(data=d)
print (df)
Data
0 [hello, she, can]
1 [not, no, more, to]
stopwords_list = set(['no','not'])
df['Data'] = [' '.join(wrd for wrd in x if wrd not in stopwords_list) for x in df['Data']]
print (df)
Data
0 hello she can
1 more to
可以使代码的重复性降低一些,我们可以省去一些事情(拨通library(ggplot2)
ggplot() +
geom_point(mapping = aes(x = x.v, y = y.v),
data = df, colour = "blue") +
geom_smooth(mapping = aes(x = x.v, y = y.v, colour = "line of best fit"),
data = df, method = "lm", show.legend = NA) +
geom_abline(mapping = aes(intercept = Inter, slope = Slope, colour = "y = x"),
data = straight.line, show.legend = FALSE) +
guides(fill = "none", linetype = "none", shape = "none", size = "none")
调用):
guide
为什么我们需要在这里使用ggplot(data = df, mapping = aes(x = x.v, y = y.v)) +
geom_point(colour = "blue") +
geom_smooth(aes(colour = "line of best fit"), method = "lm") +
geom_abline(mapping = aes(intercept = Inter, slope = Slope, colour = "y = x"),
data = straight.line, show.legend = FALSE)
而不是show.legend = FALSE
?
从文档中:
show.legend
逻辑。该图层应包括在图例中吗? NA,默认值,包括是否映射了任何美学。 FALSE从不包含,而TRUE始终包含。它也可以是命名逻辑向量,以精细选择要显示的美学效果
这意味着我们使用show.legend = NA
进行show.legend = NA
调用,我们在图例中使用了这一层。但是,我们不想使用此层,因此需要geom_abline
。您会看到这并不影响图例中包括哪些颜色,仅影响图层。
数据
show.legend = FALSE