R (ggparcoord)平行坐标图

时间:2021-06-01 22:46:05

I am facing a somewhat strange situation while plotting a parallel co-ordinates plot using ggparcoord. I am running the following code and it is running perfectly fine:

我正面临着一种有点奇怪的情况,同时绘制了一个使用ggparcoord的平行坐标图。我正在运行下面的代码,它运行得非常好:

# Load required packages
require(GGally)

# Load datasets
data(state)
df <- data.frame(state.x77,
                 State = state.name,
                 Abbrev = state.abb,
                 Region = state.region,
                 Division = state.division
) 

# Generate basic parallel coordinate plot
p <- ggparcoord(data = df,                 
                # Which columns to use in the plot
                columns = 1:4,                 
                # Which column to use for coloring data
                groupColumn = 11,                 
                # Allows order of vertical bars to be modified
                order = "anyClass",                
                # Do not show points
                showPoints = FALSE,                
                # Turn on alpha blending for dense plots
                alphaLines = 0.6,                
                # Turn off box shading range
                shadeBox = NULL,                
                # Will normalize each column's values to [0, 1]
                scale = "uniminmax" # try "std" also
)

# Start with a basic theme
p <- p + theme_minimal()

# Decrease amount of margin around x, y values
p <- p + scale_y_continuous(expand = c(0.02, 0.02))
p <- p + scale_x_discrete(expand = c(0.02, 0.02))

# Remove axis ticks and labels
p <- p + theme(axis.ticks = element_blank())
p <- p + theme(axis.title = element_blank())
p <- p + theme(axis.text.y = element_blank())

# Clear axis lines
p <- p + theme(panel.grid.minor = element_blank())
p <- p + theme(panel.grid.major.y = element_blank())

# Darken vertical lines
p <- p + theme(panel.grid.major.x = element_line(color = "#bbbbbb"))

# Move label to bottom
p <- p + theme(legend.position = "bottom")

# Figure out y-axis range after GGally scales the data
min_y <- min(p$data$value)
max_y <- max(p$data$value)
pad_y <- (max_y - min_y) * 0.1

# Calculate label positions for each veritcal bar
lab_x <- rep(1:4, times = 2) # 2 times, 1 for min 1 for max
lab_y <- rep(c(min_y - pad_y, max_y + pad_y), each = 4)

# Get min and max values from original dataset
lab_z <- c(sapply(df[, 1:4], min), sapply(df[, 1:4], max))

# Convert to character for use as labels
lab_z <- as.character(lab_z)

# Add labels to plot
p <- p + annotate("text", x = lab_x, y = lab_y, label = lab_z, size = 3)

# Display parallel coordinate plot
print(p)

I get the following output:

我得到如下输出:

R (ggparcoord)平行坐标图

The moment I want to subset the data to display fewer region levels using the following statement:

在我想用下面的语句将数据集中显示更少的区域级别时:

df<-df[which(df$Region %in% c('South','West','Northeast')),]

I start receiving the following error:

我开始收到以下错误:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

Why am I getting this error when the number of levels I want to display are clearly more than 2? Any help on this would be much appreciated.

当我要显示的层次数明显大于2时,为什么会出现这个错误?对此事的任何帮助都将不胜感激。

1 个解决方案

#1


2  

I figured what the problem was. I had to convert the column into factor.

我知道问题出在哪里。我必须把列转换成因子。

df$Region <- factor(df$Region)

The above piece of code fixes the error.

上面的代码修复了错误。

#1


2  

I figured what the problem was. I had to convert the column into factor.

我知道问题出在哪里。我必须把列转换成因子。

df$Region <- factor(df$Region)

The above piece of code fixes the error.

上面的代码修复了错误。