1. 程式人生 > >R繪製雷達圖

R繪製雷達圖

首先需要包fmsb

library(fsmb)

需要函式radarchart()

有這樣的資料

Usage

radarchart(df, axistype, seg, pty, pcol, plty, plwd, pdensity, pangle, pfcol,  cglty, cglwd, cglcol, axislabcol, title, maxmin, na.itp, centerzero,  vlabels, vlcex, caxislabels, calcex, paxislabels, palcex, ...)

Arguments

df

The data frame to be used to draw radarchart. If maxmin is TRUE, this must include maximum values as row 1 and minimum values as row 2 for each variables, and actual data should be given as row 3 and lower rows. The number of columns (variables) must be more than 2.

axistype

The type of axes, specified by any of 0:5. 0 means no axis label. 1 means center axis label only. 2 means around-the-chart label only. 3 means both center and around-the-chart (peripheral) labels. 4 is *.** format of 1, 5 is *.** format of 3. Default is 0.

seg

The number of segments for each axis (default 4).

pty

A vector to specify point symbol: Default 16 (closed circle), if you don't plot data points, it should be 32. This is repeatedly used for data series.

pcol

A vector of color codes for plot data: Default 1:8, which are repeatedly used.

plty

A vector of line types for plot data: Default 1:6, which are repeatedly used.

plwd

A vector of line widths for plot data: Default 1, which is repeatedly used.

pdensity

A vector of filling density of polygons: Default NULL, which is repeatedly used.

pangle

A vector of the angles of lines used as filling polygons: Default 45, which is repeatedly used.

pfcol

A vector of color codes for filling polygons: Default NA, which is repeatedly usd.

cglty

Line type for radar grids: Default 3, which means dotted line.

cglwd

Line width for radar grids: Default 1, which means thinnest line.

cglcol

Line color for radar grids: Default "navy"

axislabcol

Color of axis label and numbers: Default "blue"

title

if any, title should be typed.

maxmin

Logical. If true, data frame includes possible maximum values as row 1 and possible minimum values as row 2. If false, the maximum and minimum values for each axis will be calculated as actual maximum and minimum of the data. Default TRUE.

na.itp

Logical. If true, items with NA values are interpolated from nearest neighbor items and connect them. If false, items with NA are treated as the origin (but not pointed, only connected with lines). Default FALSE.

centerzero

Logical. If true, this function draws charts with scaling originated from (0,0). If false, charts originated from (1/segments). Default FALSE.

vlabels

Character vector for the names for variables. If NULL, the names of the variables as colnames(df) are used. Default NULL.

vlcex

Font size magnification for vlabels. If NULL, the font size is fixed at text()'s default. Default NULL.

caxislabels

Character vector for center axis labels, overwriting values specified in axistype option. If NULL, the values specified by axistype option are used. Default is NULL.

calcex

Font size magnification for caxislabels. If NULL, the font size is fixed at text()'s default. Default NULL.

paxislabels

Character vector for around-the-chart (peripheral) labels, overwriting values specified in axistype option. If NULL, the values specified by axistype option are used. Default is NULL.

palcex

Font size magnification for paxislabels. If NULL, the font size is fixed at text()'s default. Default NULL

英文講得很清楚了

附錄貼上實戰畫圖程式碼

at11<-
  dat %>%
  group_by(gender) %>%
  select(iid, gender, attr1_1, sinc1_1, intel1_1, fun1_1, amb1_1, shar1_1) %>% 
  unique()


#Next, we would like to turn all NA into 0, but before this, we check if any entries in iid or gender is NA to prevent mislabels


#Apply command to change all NA to 0


at11[is.na(at11)] <- 0


#Add column to check if total of attributions add up to 100


at11$total <- rowSums(at11[,c("attr1_1", "sinc1_1", "intel1_1", "fun1_1", "amb1_1", "shar1_1")])


#A total of 0 means all entries are missing and row is dropped


at11<-
  at11 %>% 
  filter(!total == "0")


#As there are entry errors in the data, all points are redistributed and curved to fit 100 total points


at11$attr1_1 <- round(at11$attr1_1/at11$total*100, digits = 2)
at11$sinc1_1 <- round(at11$sinc1_1/at11$total*100, digits = 2)
at11$intel1_1 <- round(at11$intel1_1/at11$total*100, digits = 2)
at11$fun1_1 <- round(at11$fun1_1/at11$total*100, digits = 2)
at11$amb1_1 <- round(at11$amb1_1/at11$total*100, digits = 2)
at11$shar1_1 <- round(at11$shar1_1/at11$total*100, digits = 2)


at11$total <- rowSums(at11[,c("attr1_1", "sinc1_1", "intel1_1", "fun1_1", "amb1_1", "shar1_1")])


at11$total <- round(at11$total, digits = 0)
#Next, data is visualized using a radar chart
#Obtain female group's and male group's mean value of each variable
test1 <-
  at11 %>%
  group_by(gender) %>%
  summarise(Attractive = mean(attr1_1), Sincere = mean(sinc1_1), Intelligent = mean(intel1_1), Fun = mean(fun1_1), Ambitious = mean(amb1_1), Interest = mean(shar1_1))

#The varialbe gender is excluded in the dataframe for plot
test1forplot <-
  test1 %>% 
  select(-gender)
#Set the maximum and minimum in the radar chart
maxmin <- data.frame(
  Attractive = c(36, 0),
  Sincere = c(36, 0),
  Intelligent = c(36, 0),
  Fun = c(36, 0),
  Ambitious = c(36, 0),
  Interest = c(36, 0))

#attach the maximum and minimum to the dataframe for plot
test11 <- rbind(maxmin, test1forplot)
#rename the dataframe
names(test11) <-c('吸引力','真誠','智力','幽默','雄心','愛好') 
#the dataframe for male group and for female group
test11male <- test11[c(1,2,4),]
test11female <- test11[c(1,2,3),]
#plot the radar chart
radarchart(test11,
           pty = 32,
           axistype = 0,
           pcol = c(adjustcolor("hotpink1", 0.5), adjustcolor("cadetblue2", 0.5)),
           pfcol = c(adjustcolor("hotpink1", 0.5), adjustcolor("cadetblue2", 0.5)),
           plty = 1,
           plwd = 3,
           cglty = 1,
           cglcol = "gray88",
           centerzero = TRUE,
           seg = 5,
           vlcex = 0.75,
           palcex = 0.75)
#attach the legend to the radar chart
legend("topleft", 
       c("男性", "女性"),
       fill = c(adjustcolor("cadetblue2", 0.5), adjustcolor("hotpink1", 0.5)))