如何使用ggplot2在R中为图形中的标记区域创建图例
嘿,伙计们,我正试图绘制一些光谱数据,我修改了这些数据。我正在使用如何使用ggplot2在R中为图形中的标记区域创建图例,r,ggplot2,plot,graph,R,Ggplot2,Plot,Graph,嘿,伙计们,我正试图绘制一些光谱数据,我修改了这些数据。我正在使用ggplot2绘制图形 graphik <- ggplot(data = spektrum2, mapping=aes(x=y0000,y=x0001))+ geom_rect(mapping=aes(xmin=285, xmax=290.3, ymin=min(x0001-.5), ymax=max(x0001+0.5),`fill="orange")) + #geom_text(aes(x=
ggplot2
绘制图形
graphik <- ggplot(data = spektrum2, mapping=aes(x=y0000,y=x0001))+
geom_rect(mapping=aes(xmin=285, xmax=290.3, ymin=min(x0001-.5), ymax=max(x0001+0.5),`fill="orange")) +
#geom_text(aes(x=288+(288.5-290.3)/2, y=min(x0001)+(max(x0001)-min(x0001))/2, label="Carbon-edge",color="brown",vjust=-30), size=4) +
geom_rect(mapping=aes(xmin=524, xmax=539, ymin=min(x0001-.5), ymax=max(x0001+0.5), fill="yellow")) +
#geom_text(aes(x=539+(524-539)/2, y=min(x0001)+(max(x0001)-min(x0001))/2, label="Oxygen-edge",color="brown",vjust=-30), size=4) +
geom_line(mapping=aes(color="blue"))+
labs(title = "Isoprene#8", title.position="center")+
coord_cartesian(xlim = c(270, 570), ylim = c(0, 1.4))+
ylab("Optical Desity [-]")+
xlab("Energy [eV]")+
scale_x_continuous(breaks=c(seq(270,580, 20)))+
scale_y_continuous(breaks=c(seq(0,2, 0.1)))+
theme(plot.title = element_text(hjust = 0.5))+
stat_peaks(aes(color = "red"),shape = 20, span = 14, size = 2)+
stat_label_peaks(geom = "label_repel", span = 10, segment.color = "black", size = 3, vjust = 1, nudge_y = 0.8, direction = "y") +
scale_color_identity()+
scale_fill_identity()
print(graphik)```
graphik好,没有数据也没有绘图,只能猜测。但是有一个家伙在为他的几何图形建立一个图例的问题上得到了答案,也许这会有帮助:尝试使用scale\u fill/color\u identity(guide=“legend”)
。默认情况下,“scale\u xxx\u identity”不会生成图例。非常感谢@stefan,它工作正常!!:)谢谢@Sirius这是一个很好的信息!