在条形图文本中的条形上方添加文本

我的代码的优先级是(MWE):

# https://www.kaggle.com/kaggle/kaggle-survey-2017/data

#### Analisis primario del dataset ####
response <- read.csv(file = "multipleChoiceResponses.csv",na.strings = "")

# seleccionamos solo algunas variables :
Variables <- c("GenderSelect","Country","Age","CurrentJobTitleSelect","MLToolNextYearSelect","LanguageRecommendationSelect","FormalEducation","FirstTrainingSelect","EmployerIndustry")

# Mantenemos en memoria solo las variables seleecionadas : 
response <- response[,Variables]

# Por un tema de cantidades solo nos quedamos con M y F 
Response <- response[response$GenderSelect == "Male" | response$GenderSelect == "Female",]

# agrego una columna para los continenetes (continent) a donde pertenecen los paises (Country)
library(countrycode)
Response$continent <- countrycode(sourcevar = Response[,"Country"],origin = "country.name",destination = "continent")

# Convertimos a factor esta nueva variable
Response$continent <- as.factor(Response$continent)


# Eliminamos las filas con elementos NA 
Response <- Response[complete.cases(Response),]

# Enumeramos todas las filas de manera adecuada
rownames(Response) <- 1:nrow(Response)


Response <- droplevels(Response)


bp_Continent <- barplot(table(Response$continent),main = "Distribucion de DS por continentes",ylim = c(0,3500)
)

# Add GenderSelect proportion by continent  in label argument ("BLABLABLA")
text(x = bp_Continent,y = table(Response$continent),label = "BLABLABLA",pos = 3,cex = 0.8,col = "red")

基本上,脚本会加载数据,选择一些变量,创建新变量(大陆)以最终清理数据。接下来要做的是创建一个条形图,将男人和女人的比例放在条形图上

在条形图文本中的条形上方添加文本

我想要做的是按大陆将“ BLABLABLA”更改为男女比例(GenderSelect变量)。

我的问题根本不类似于: How to display the frequency at the top of each factor in a barplot in R

因为我感兴趣的是比例和条形上方印象的计算。

qwe541171815 回答:在条形图文本中的条形上方添加文本

下面的代码使用最终创建的组合数据集。
计算完比例后,只需向其传递函数text,参数label

计算比例。

tbl <- table(Response$continent)
xt <- xtabs( ~ GenderSelect + continent,Response)
prop <- sweep(xt,2,tbl,`/`)

现在绘制条形图。标签是"Male"的比例。

bp_Continent <- barplot(tbl,main = "Distribucion de DS por continentes",ylim = c(0,3500)
)
text(x = bp_Continent,y = tbl,label = round(prop[2,],2),pos = 3,cex = 0.8,col = "red")

enter image description here

其他标签可以是例如:

sprintf("F: %1.2f/M: %1.2f",prop[1,prop[2,])

数据创建代码。

set.seed(1234)
n <- 5e3
GenderSelect <- c("Male","Female")
GenderSelect <- sample(GenderSelect,n,TRUE)
continent <- c("Africa","Americas","Asia","Europa","Oceania")
continent <- sample(continent,TRUE,prob = c(1,20,14,16,2))
Response <- data.frame(GenderSelect,continent)
,

在阅读Rui的答案后,我想到了另一种解决方案。

首先是一个计算男女比例(按大陆划分)的函数,然后应用。

function handleFormSubmit(event) {
   // This next line prevents the reload of the form
   event.preventDefault();
   // Get values of inputs
   // Pass values to addNewPost()
   var username = document.getElementById("formUsername").value;
   var caption = document.getElementById("formCaption").value;
   var formImg = document.getElementById("formImg").value;

   addNewPost(username,caption,formImg ); // call the function here.
}
function addNewPost(username,imgLocation) {


}

并完成:

CreaEtiq <- function(conti){
  NumHContin <- dim(Response[Response$GenderSelect=="Male" & Response$continent==conti,])[1]
  NumMACntin <- dim(Response[Response$GenderSelect=="Female" & Response$continent==conti,])[1]
  return(round(NumHContin/NumMACntin,2))
}
EtiquetaBarPlot <- sapply(levels(Response$continent),CreaEtiq)

获得下图

enter image description here

本文链接:https://www.f2er.com/2985264.html

大家都在问