ggplot2– Practice (change of legend: Take the frequency scatter graph as an example) Ggplot2 direct realization
Paint code:
The data is two columns and the second is the factor type.
> Head (plotdat)
Soc Soc Low alarm
1 0 normal
2 2 error
3 0 Normal
4 1 error
5 0 normal
# # #大小和高度都代表频率 ###
g <-g + geom_point (Aes (x= plotdat[,1], y=. Density., colour=plotdat[,2], shape=plotdat[,2]),
stat= "bin", binwidth=0.1, Size=3, alpha=0.6) # # With stat conversion (Ggplot2 1.1 is introduced), Binwidth for bandwidth, size is the point of magnitude, alpha is transparency
g <-G + Labs (x = "Soc", y = "count", title= "Soc Frequency Scatter chart")
# #设定名字
Problem
There are three questions about the legend:
1. How to remove the legend.
2. The legend is double mapped and how to modify it.
3. How to add your own legend (using layer properties) 1 to remove the legend
How to remove the legend, using the mapping attribute scale, add the parameter guide= "None". 2 Modify Legend
Modified as a single map, and dual mappings are modified for dual mappings.
G <-Ggplot (Plotdat)
g <-G + geom_point (Aes (x=plotdat[,1), size=. Density., colour=plotdat[,2], shape=plotdat[,2]),
stat= "bin", binwidth=0.1, Size=3, alpha=0.6)
g <-g + Labs (x = "Soc", y = "count", title= "soc frequency scatter graph")
g <-g + Scale_colour_hue (name= "Soc Low alarm")
g <-g + Scale_sha Pe_manual (Values=c (17,19), name= "Soc Low alarm")
g
3 Add your own legend
From the way you can see the direct use of ggplot2 to draw the frequency scatter plot, the bottom will draw bandwidth. The data will be processed to solve this problem. Note the position of the colour in the following code, which will produce a legend inside the mapping.
Plot_normal <-Plotdat[which (plotdat[,2]== "normal"), Plot_error <-
(plotdat[which "error"), ]
plot_nor <-as.data.frame (table (plot_normal[,i))
plot_err <-(table (as.data.frame))
plot_nor[,1] <-as.numeric (plot_nor[,1])
plot_err[,1] <-as.numeric (plot_err[,1))
G <-Ggplot (
g <-G + geom_point (Aes (x=plot_nor[,1), y=plot_nor[,2], colour= "Blue"), size=3, data=plot_nor, alpha = 0.6)
G <-G + geom_point (Aes (x=plot_err[,1), y=plot_err[,2], colour= "Red"), size=3, data=plot_err, alpha = 0.6)
g <- G + Labs (x = names (Plotdat) [i], y = "count", title= "soc frequency scatter graph")
g <-g + scale_colour_hue (name = "Soc Low alarm", labels = C ("Normal", "error")
print (g)