我有以下简单示例,使用不同的颜色为每个带有图例的类显示线图.
A = [
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
11 12 13 14
15 16 17 18
19 18 17 16
15 14 13 12
];
B = [1 1 1 1 2 2 2 2 ];
p1=plot(A(B==1,:)','r');
hold on
p2=plot(A(B==2,:)','b');
legend([p1;p2],'Red lines','Blue lines');
Run Code Online (Sandbox Code Playgroud)
但该脚本发出警告信息说
Warning: Ignoring extra legend entries.
> In legend at 291
Run Code Online (Sandbox Code Playgroud)
两个图例用相同颜色的线条显示.这是怎么回事?

我试图绘制一条由黄土平滑的线,但我试图找出如何包含由现有变量定义的阴影错误区域,但也进行了平滑。
此代码创建示例数据:
set.seed(12345)
data <- cbind(rep("A", 100), rnorm(100, 0, 1))
data <- rbind(data, cbind(rep("B", 100), rnorm(100, 5, 1)))
data <- rbind(data, cbind(rep("C", 100), rnorm(100, 10, 1)))
data <- rbind(data, cbind(rep("D", 100), rnorm(100, 15, 1)))
data <- cbind(rep(1:100, 4), data)
data <- data.frame(data)
names(data) <- c("num", "category", "value")
data$num <- as.numeric(data$num)
data$value <- as.numeric(data$value)
data$upper <- data$value+0.20
data$lower <- data$value-0.30
Run Code Online (Sandbox Code Playgroud)
绘制下面的数据,这就是我得到的:
ggplot(data, aes(x=num, y=value, colour=category)) +
stat_smooth(method="loess", se=F)
Run Code Online (Sandbox Code Playgroud)

我想要的是一个如下图所示的图,除了阴影区域的上限和下限由生成数据中的“上”和“下”变量的平滑线界定。

任何帮助将不胜感激。
我想使用ggplot2用5条不同的线绘制线图。我用下面的代码。
plot <- ggplot() +
geom_line(data=MS, aes(x=date, y=MSCI.World.PI, color='MS')) +
geom_line(data=S, aes(x=date, y=SandP.TR, color='S')) +
geom_line(data=BR, aes(x=date, y=MSCI.BRIC.PI, color='BR')) +
geom_line(data=HF, aes(x=date, y=HFRX, color='HF')) +
geom_line(data=LP, aes(x=date, y=LPX50.TR, color='LP')) +
scale_color_manual(values = c("red", "blue", "green", "yellow", "violet" )) +
labs(color="Indices") +
xlab('Time') +
ylab('Price')
plot
Run Code Online (Sandbox Code Playgroud)
“错误”部分是,未按预期对颜色进行排序,这意味着第一行(“ MS”)未分配给第一颜色(“红色”)。似乎该行已按字母顺序分配给了颜色。
我有什么办法可以更改分配,以便在scale_color_manuel语句中将第一行设置为第一种颜色,将第二行设置为第二种颜色,依此类推?
我有两张表,一张是从热图生成的,一张是需要在辅助 y 轴上绘制折线图的。创建热图没有问题:
green = sns.light_palette("seagreen", reverse=True, as_cmap=True)
green.set_over('tomato')
sns.set(rc={'figure.figsize': (20.7, 10.27)})
sns.set(font_scale=2)
ax=sns.heatmap(df, square=True, linewidths=.5, annot=False, fmt='.3f',
cmap=green, vmin=0, vmax=0.05)
Run Code Online (Sandbox Code Playgroud)
当我尝试在热图顶部绘制线条时,问题就开始了。该线应具有相同的 x 轴值,并且这些值应位于辅助 y 轴中。df 行如下所示:
>>>day value
0 14 315.7
1 15 312.3
2 16 305.9
3 17 115.2
4 18 163.2
5 19 305.78
...
Run Code Online (Sandbox Code Playgroud)
我尝试将其绘制在顶部,如下所述:
green = sns.light_palette("seagreen", reverse=True, as_cmap=True)
green.set_over('tomato')
sns.set(rc={'figure.figsize': (20.7, 10.27)})
sns.set(font_scale=2)
ax=sns.heatmap(df, square=True, linewidths=.5, annot=False, fmt='.3f',
cmap=green, vmin=0, vmax=0.05)
ax2=plt.twinx()
ax2.plot(df_line['day'], df_line['value'],color="blue")
line = ax2.lines[0]
line.set_xdata(line.get_xdata() + 0.5)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但后来我把线“移”到了左侧,我在 …
假设以下数据框:
mydf <- data.frame(date = as.Date(rep(c('2019-11-01', '2019-10-01'), 2)),
value = c(10, 15, 8, 4),
type = c('Type 1', 'Type 1', 'Type 2', 'Type 2'))
print(mydf)
date value type
1 2019-11-01 10 Type 1
2 2019-10-01 15 Type 1
3 2019-11-01 8 Type 2
4 2019-10-01 4 Type 2
Run Code Online (Sandbox Code Playgroud)
我想创建一个自动代码,为每种类型创建一个线图并定义每条线的颜色。一般来说,我知道该怎么做:
require(ggplot2)
myplot <- ggplot(mydf, aes(x = date, y = value, colour = type)) + geom_line() +
scale_color_manual(name = 'Type', values=c('blue', 'red'))
Run Code Online (Sandbox Code Playgroud)
但是,在另一个月运行代码时,数据框可能会发生变化。Type 3数据框中可能有:
mydf <- data.frame(date = as.Date(rep(c('2019-11-01', '2019-10-01'), …Run Code Online (Sandbox Code Playgroud)