如何从此代码生成的图例中删除字母"a"?如果我删除了geom_text
,那么'a'字母将不会显示在图例中.不过我想保留geom_text
.
ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape = Species, colour = Species)) +
geom_point() +
geom_text(aes(label = Species))
Run Code Online (Sandbox Code Playgroud) 我想制作一个图,其中X值作为测量的子集,Y值作为测量数据的另一个子集.
在下面的例子中,我有4个产品p1,p2,p3和p4.每个都根据他们的歪斜,颜色和版本定价.我想创建一个多面图,描绘P3产品(Y轴)与P1产品(X轴).
我的下面的尝试失败了,出现以下错误:
错误:美学必须是长度为1或与dataProblems相同的长度:子集(price,product =="p1"),子集(price,product =="p3")
library(ggplot2)
product=c("p1","p1","p1","p1","p1","p1","p1","p1","p2","p2","p2","p2","p2","p2","p2","p2","p3","p3","p3","p3","p3","p3","p3","p3","p4","p4","p4","p4","p4","p4","p4","p4")
skew=c("b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a")
version=c(0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2)
color=c("C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2")
price=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)
df = data.frame(product, skew, version, color, price)
# First plot all the data
p1 <- ggplot(df, aes(x=price, y=price, colour=factor(skew))) + geom_point(size=2, shape=19)
p1 <- p1 + facet_grid(version ~ color)
p1 # This gavea very good plot. So far so good
# Now plot P3 vs P1
p1 <- ggplot(df, aes(x=subset(price, product=='p1'), y=subset(price, product=='p3'), colour=factor(skew))) + geom_point(size=2, shape=19)
p1
# failed with: Error: Aesthetics must either be length one, …
Run Code Online (Sandbox Code Playgroud) 随着失踪信息学背景有困难明白之间的差别aes
,并aes_string
在GGPLOT2及其对日常使用影响.
从描述(?aes_string
)我能够理解两者describe how variables in the data are mapped to visual properties (aesthetics) of geom
.
此外,它说,aes uses non-standard evaluation to capture the variable names.
而aes_string
使用regular evaluation
.
从示例代码可以看出,两者都产生相同的输出(a list of unevaluated expressions
):
> aes_string(x = "mpg", y = "wt")
List of 2
$ x: symbol mpg
$ y: symbol wt
> aes(x = mpg, y = wt)
List of 2
$ x: symbol mpg
$ y: …
Run Code Online (Sandbox Code Playgroud) 对于数据:
def.percent period valence
1 6.4827843 1984-1985 neg
2 5.8232425 1985-1986 neg
3 -2.4003260 1986-1987 pos
4 -3.5994399 1987-1988 pos
Run Code Online (Sandbox Code Playgroud)
如果我在点上添加一条线,我怎样才能使用ggplot2为线条[geom_area()]下的区域着色,使用不同的颜色为"neg"和"pos"?
我试过这个:
ggplot(data, aes(x=period, y=def.percent, group = 1)) +
geom_area(aes(fill=valence)) +
geom_line() + geom_point() + geom_hline(yintercept=0)
Run Code Online (Sandbox Code Playgroud)
但是R返回错误:
Error: Aesthetics can not vary with a ribbon
Run Code Online (Sandbox Code Playgroud)
这个相同的代码适用于不同的数据集,我不明白这里发生了什么,例如:
library(gcookbook) # For the data set
cb <- subset(climate, Source=="Berkeley")
cb$valence[cb$Anomaly10y >= 0] <- "pos"
cb$valence[cb$Anomaly10y < 0] <- "neg"
ggplot(cb, aes(x=Year, y=Anomaly10y)) +
geom_area(aes(fill=valence)) +
geom_line() +
geom_hline(yintercept=0)
Run Code Online (Sandbox Code Playgroud) 我最近一直在使用样式表matplotlib
。我真的很喜欢seaborn-white
外观的干净程度,我希望能够将边框添加到其他样式中,例如ggplot
或seaborn-whitegrid
。
如何在我的ax
对象周围添加黑色边框fig, ax = plt.subplots()
?
import pandas as pd
import numpy as np
from collections import *
Se_data = pd.Series(Counter(np.random.randint(0,10,100)))
with plt.style.context("seaborn-whitegrid"):
fig, ax = plt.subplots()
Se_data.plot(kind="barh", ax=ax, title="No Border")
with plt.style.context("seaborn-white"):
fig, ax = plt.subplots()
Se_data.plot(kind="barh", ax=ax, title="With Border")
Run Code Online (Sandbox Code Playgroud)
回应以下答案:
Se_data = pd.Series(Counter(np.random.randint(0,10,100)))
with plt.style.context("seaborn-whitegrid"):
fig, ax = plt.subplots()
Se_data.plot(kind="barh", ax=ax, title="No Border")
ax.spines['bottom'].set_color('0.5')
ax.spines['top'].set_color(None)
ax.spines['right'].set_color('0.5')
ax.spines['left'].set_color(None)
ax.patch.set_facecolor('0.1')
plt.grid(b=True, which='major', color='0.2', linestyle='-')
plt.grid(b=True, which='minor', color='0.2', linestyle='-')
ax.tick_params(axis='x', …
Run Code Online (Sandbox Code Playgroud) 我想从ggplot的填充图例中删除颜色线.我通常guide_legend(override.aes = ...)
用来修改传奇美学 - 适用于点,线,alpha等,但它不适用于我的color
审美.我究竟做错了什么?
# generate data
set.seed(47)
data = data.frame(year = rep(2000:2004, 3),
value = runif(15),
group = rep(c("A", "B", "C"), each = 5))
# create the plot
p = ggplot(data, aes(x = year, y = value, fill = group)) +
geom_area(position = position_fill(), color = "white") +
scale_fill_grey()
# this should modify the fill legend to remove the colored line
# but the line is still there
p + guides(fill = guide_legend(override.aes = …
Run Code Online (Sandbox Code Playgroud) 给定以下数据框:
import pandas as pd
df = pd.DataFrame({
"n_index": list(range(5)) * 2,
"logic": [True] * 5 + [False] * 5,
"value": list(range(5)) + list(range(5, 10))
})
Run Code Online (Sandbox Code Playgroud)
我想只用颜色来区分logic
线条图中的颜色,并在value
s 上标记点。具体来说,这是我想要的输出(由R ggplot2绘制):
ggplot(aes(x = n_index, y = value, color = logic), data = df) + geom_line() + geom_point()
Run Code Online (Sandbox Code Playgroud)
我尝试使用做相同的事情seaborn.lineplot
,并且指定了markers=True
但没有标记:
import seaborn as sns
sns.set()
sns.lineplot(x="n_index", y="value", hue="logic", markers=True, data=df)
Run Code Online (Sandbox Code Playgroud)
然后,我尝试添加style="logic"
代码,现在标记出现了:
sns.lineplot(x="n_index", y="value", hue="logic", style="logic", markers=True, data=df)
Run Code Online (Sandbox Code Playgroud)
我也尝试将标记设置为相同样式:
sns.lineplot(x="n_index", …
Run Code Online (Sandbox Code Playgroud) 我用 ggplot2 制作了散点图,并将二进制变量映射到点大小。结果令人满意,但我收到警告“不建议使用离散变量的大小”。
据我所知,使用大小来映射具有多个级别的非序数分类变量可能不如使用点形状或不同颜色清晰。然而,我想知道这个警告是否是为了警告我们有更严重的事情。
是否有比使用 aes(size=...) 更明智的方法来根据二进制或分类变量更改点大小?
警告“不建议使用离散变量的大小”只是一个设计技巧吗?
如果我的结果看起来不错,那么下次我想要在相似数据上使用相同类型的图形时,我是否应该担心该警告?
假设我更喜欢默认geom_point
使用circle(pch=1
)而不是solid dots(pch=16
).您可以通过传递shape
参数来更改标记的形状geom_point
,例如
ggplot(diamonds, aes(depth, carat, colour=cut)) + geom_point(shape=1)
ggplot(diamonds, aes(depth, carat, colour=cut)) + geom_point(shape=16)
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何更改默认行为.
有谁知道如何改变seaborn的传奇和头衔?见下文.我有点想把"Gaussia"改为"Guassian Naive Bayes"等......
或第二张图片中的图例
aesthetics ×10
ggplot2 ×7
r ×7
python ×3
seaborn ×2
border ×1
default ×1
matplotlib ×1
plot ×1
scatter-plot ×1