Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ggplot2在R中为图形中的标记区域创建图例_R_Ggplot2_Plot_Graph - Fatal编程技术网

如何使用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这是一个很好的信息!