简体   繁体   English

在ggplot2散点图中绘制2个数据集时在图例中出现线型问题

[英]Issue with linestyle in legend while ploting 2 datasets in ggplot2 scatter plot

I would like to compare some numerical analysis results with analytical solution in a plot using ggplot2. 我想将一些数值分析结果与使用ggplot2的图中的解析解进行比较。 I am plotting analytical solution with a continuous solid line and numerical solution with dots. 我正在画一条具有连续实线的解析解和带有点的数值解。 The only issue is in the legend, where a line is displayed for the numerical results (FVM). 唯一的问题是在图例中,其中为数值结果(FVM)显示了一行。 I only want a circle to be displayed in the legend for "FVM" not the line and circle. 我只希望在图例中为“ FVM”显示一个圆,而不是直线和圆。 Would you please help? 你能帮忙吗? here are my script and plot: 这是我的脚本和情节:

library(ggplot2)
xa <- seq(0, 1, by = .01)
phia<- -(exp(2.5*xa/0.1)-1)/(exp(2.5/0.1)-1)+1
anal.dat <-data.frame(x=xa,y=phia)
xc <- c (0.258065 , 0.645161 , 0.83871 , 0.935484 , 0.983871)
phic <- c (0.99869, 0.984046, 0.905865, 0.677627, 0.287415)
cfd.data <- data.frame(x=xc,T=phic)
p <- ggplot(data=NULL)
p + geom_line(data=anal.dat, aes(x, y,colour="Analytical"),size = 1) + geom_point(data=cfd.data,aes(x, T,colour="FVM"),size = 4,shape=16) + theme_bw() + labs(x = "x",y=expression(phi)) + scale_colour_manual(name='', values=c('Analytical'='deepskyblue','FVM'='firebrick1'))

在此处输入图片说明

Use guide_legend to override the current legend. 使用guide_legend覆盖当前图例。

p + geom_line(data=anal.dat, aes(x, y,colour="Analytical"),size = 1) +
  geom_point(data=cfd.data,aes(x, T,colour="FVM"),size = 4,shape=16) +
  theme_bw() + labs(x = "x",y=expression(phi)) +
  scale_colour_manual(name='', values=c('Analytical'='deepskyblue','FVM'='firebrick1')) +
  guides(colour = guide_legend(override.aes = list(shape = c(NA, 19), linetype = c("solid", "blank"))))

The output is 输出是 产量

Here is the list of symbols numbers (I used 19). 这是符号编号列表 (我用过19)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM