Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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
使用HH软件包控制likert图中的条形标签_R_Graph_Label_Rstudio_Likert - Fatal编程技术网

使用HH软件包控制likert图中的条形标签

使用HH软件包控制likert图中的条形标签,r,graph,label,rstudio,likert,R,Graph,Label,Rstudio,Likert,我是R的初学者,正在使用RStudio创建一个likert图,其中的百分比显示在条形图上。一切正常,但与正确显示的“很少”和“有时”的百分比不同,“大多数时间”和“几乎总是”的百分比都不在图表上的正确位置。例如,S11的“大部分时间”和“几乎总是”分别为28%和14%,但图表显示“大部分时间”和“几乎总是”分别为14%和28% 以下是我的意见: library(HH) # store the original col names used in custom panel function o

我是R的初学者,正在使用RStudio创建一个likert图,其中的百分比显示在条形图上。一切正常,但与正确显示的“很少”和“有时”的百分比不同,“大多数时间”和“几乎总是”的百分比都不在图表上的正确位置。例如,S11的“大部分时间”和“几乎总是”分别为28%和14%,但图表显示“大部分时间”和“几乎总是”分别为14%和28%

以下是我的意见:

library(HH)

# store the original col names used in custom panel function
origNames = colnames(x)

# define a custom panel function
myPanelFunc <- function(...){
panel.likert(...)
vals <- list(...)
DF <- data.frame(x=vals$x, y=vals$y, groups=vals$groups)

### some convoluted calculations here...
grps <- as.character(DF$groups)
for(i in 1:length(origNames)){
grps <- sub(paste0('^',origNames[i]),i,grps)
}

DF <- DF[order(DF$y,grps),]

DF$correctX <- ave(DF$x,DF$y,FUN=function(x){
x[x < 0] <- rev(cumsum(rev(x[x < 0]))) - x[x < 0]/2
x[x > 0] <- cumsum(x[x > 0]) - x[x > 0]/2
return(x)
})

subs <- sub(' Positive$','',DF$groups)
collapse <- subs[-1] == subs[-length(subs)] & DF$y[-1] == DF$y[- 
length(DF$y)]
DF$abs <- abs(DF$x)
DF$abs[c(collapse,FALSE)] <- DF$abs[c(collapse,FALSE)] + 
DF$abs[c(FALSE,collapse)]
DF$correctX[c(collapse,FALSE)] <- 0
DF <- DF[c(TRUE,!collapse),]

DF$perc <- round(ave(DF$abs,DF$y,FUN=function(x){x/sum(x) * 100}), 0)


## Here goes 6 lines that have been changes - AK
# here we modify the column with labels a bit:
DF$perc <- paste0(DF$perc,'%')
# change all "0%" to blanks
DF$perc[DF$perc == "0%"] <- ""
# the argument label is a bit modified too
panel.text(x=DF$correctX, y=DF$y, label=DF$perc, cex=0.7)
}


# plot passing our custom panel function

p <- plot.likert(x, 
as.percent=TRUE, 
main = "Graph title",
positive.order = T,
ylab = "Question",    
key.border.white=F,
panel=myPanelFunc,
rightAxis=F, col = c("#FF7F00", "#FFBF80", "#C1E0F4", "#82C0E9")   
)

p
库(HH)
#存储自定义面板函数中使用的原始列名称
origNames=colnames(x)
#定义自定义面板函数

myPanelFunc我认为问题出在第一行,请确保将x替换为中数据帧的名称

origNames=colnames(x)