【发布时间】:2020-10-18 06:35:58
【问题描述】:
我想用ggplot 绘制两个散点图,但我不是很熟悉。我一直在尝试遵循其他示例,但这个包的分层方法让我感到困惑。
在bothfrontier_data 中,我希望第一列是相对于第三列的 x 变量,第二列是相对于第四列的 x 变量。另外,如何向该图添加自定义轴标题并添加自定义轴范围?
谢谢
############# GGPLOT TO SHOW BOTH PLOTS SUPERIMPOSED ###################################
bothfrontier_data <- data.frame(std_portfolios_Qts, std_portfolios_Qsi,
All_Portfolio_Returns_Qts, All_Portfolio_Returns_Qsi)
head(bothfrontier_data)
# std_portfolios_Qts std_portfolios_Qsi All_Portfolio_Returns_Qts All_Portfolio_Returns_Qsi
#1 0.8273063 0.8194767 0.3421454 0.3357710
#2 0.8272188 0.8196555 0.3421551 0.3357853
#3 0.8273064 0.8192980 0.3421648 0.3357996
#4 0.8271314 0.8194769 0.3421744 0.3358139
#5 0.8272191 0.8194770 0.3421840 0.3358281
#6 0.8272193 0.8194772 0.3421935 0.3358423
dim(bothfrontier_data)
#[1] 501 4
BothFrontiers <- ggplot(bothfrontier_data, aes(x=std_portfolios_Qts)) +
geom_point(aes(y=All_Portfolio_Returns_Qts), color = "blue") +
geom_point(aes(y=All_Portfolio_Returns_Qsi), color = "red")
plot(BothFrontiers)
【问题讨论】:
-
Youn 不需要这个
plot()函数。此外,您可以直接在geom_point内传递所有aes。BothFrontiers <- ggplot(bothfrontier_data ) + geom_point(aes(x=std_portfolios_Qts, y=All_Portfolio_Returns_Qts), color = "blue") + geom_point(aes(x=std_portfolios_Qts, y=All_Portfolio_Returns_Qsi), color = "red")但我不确定您是否希望两者都在同一个情节或不同的图层中。要进行绘图,请仅调用BothFrontiers而不调用plot() -
@AurelianoGuedes 你好。是的,我希望两者都在同一个情节中,而不是彼此相邻
标签: r dataframe ggplot2 plot plotly