Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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
R 使用ggplot2中的镶嵌面网格调整数据标签位置_R_Graphics_Ggplot2_Plyr_Facet - Fatal编程技术网

R 使用ggplot2中的镶嵌面网格调整数据标签位置

R 使用ggplot2中的镶嵌面网格调整数据标签位置,r,graphics,ggplot2,plyr,facet,R,Graphics,Ggplot2,Plyr,Facet,我在R中有一个例子: 包裹 install.packages("plyr") library(plyr) install.packages("ggplot2") library(ggplot2) install.packages("ggthemes") library(ggthemes) 这是我的密码: uf <- c("AC","AC","AC","AC","AC","AC","AC","AC","AM","AM","AM", "AM","AM","AM","AM","AM

我在R中有一个例子:

包裹

install.packages("plyr")
library(plyr)

install.packages("ggplot2")
library(ggplot2)

install.packages("ggthemes")
library(ggthemes)
这是我的密码:

    uf <- c("AC","AC","AC","AC","AC","AC","AC","AC","AM","AM","AM",
"AM","AM","AM","AM","AM")

    da <- c("Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada")

    tr <- c(97,99,90.5,78.6,3,1,9.5,21.4,97.2,99.1,96.8,98.7,2.8,0.9,3.2,1.3)

    resposta <- c("resposta","resposta","resposta","resposta","não resposta","não resposta","não resposta","não resposta","resposta","resposta","resposta","resposta", 
"não resposta","não resposta","não resposta","não resposta")

    taxa <- data.frame(uf, da, tr,resposta)

######_Adjusting data labels position_###############
   taxa <- ddply(taxa, .(da), transform, pos = cumsum(tr) - (0.5 * tr)) # Ajusta a posição

   g1 <- ggplot()+ geom_bar(aes(y = tr, x = da, fill = resposta), data = taxa, 
                          stat="identity") +
                facet_wrap(~uf) +
                geom_text(data=taxa, aes(x = da, y = pos, label = tr), size=4) + 
                theme(legend.position="bottom", legend.direction="horizontal", 
                 legend.title = element_blank())

   g1

uf我整理了一下您的代码,并使用
ifelse
确定标签的正确位置

Library(ggplot2)

uf <- c("AC","AC","AC","AC","AC","AC","AC","AC","AM","AM","AM",
        "AM","AM","AM","AM","AM")

da <- c("Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada")

tr <- c(97,99,90.5,78.6,3,1,9.5,21.4,97.2,99.1,96.8,98.7,2.8,0.9,3.2,1.3)

resposta <- c("resposta","resposta","resposta","resposta","não resposta","não resposta","não resposta","não resposta","resposta","resposta","resposta","resposta", 
              "não resposta","não resposta","não resposta","não resposta")

taxa <- data.frame(uf, da, tr,resposta)

######_Adjusting data labels position_###############

taxa$pos <- ifelse(taxa$tr > 50, taxa$tr / 2, 100 -  taxa$tr / 2)

ggplot(taxa, aes(y = tr, x = da, fill = resposta, label = tr)) + 
  geom_bar(stat="identity") +
  geom_text(aes(y = pos), size=4) + 
  facet_wrap(~uf) +
  theme(legend.position="bottom", legend.direction="horizontal", 
        legend.title = element_blank())
库(ggplot2)

uf我整理了一下您的代码,并使用
ifelse
确定标签的正确位置

Library(ggplot2)

uf <- c("AC","AC","AC","AC","AC","AC","AC","AC","AM","AM","AM",
        "AM","AM","AM","AM","AM")

da <- c("Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada")

tr <- c(97,99,90.5,78.6,3,1,9.5,21.4,97.2,99.1,96.8,98.7,2.8,0.9,3.2,1.3)

resposta <- c("resposta","resposta","resposta","resposta","não resposta","não resposta","não resposta","não resposta","resposta","resposta","resposta","resposta", 
              "não resposta","não resposta","não resposta","não resposta")

taxa <- data.frame(uf, da, tr,resposta)

######_Adjusting data labels position_###############

taxa$pos <- ifelse(taxa$tr > 50, taxa$tr / 2, 100 -  taxa$tr / 2)

ggplot(taxa, aes(y = tr, x = da, fill = resposta, label = tr)) + 
  geom_bar(stat="identity") +
  geom_text(aes(y = pos), size=4) + 
  facet_wrap(~uf) +
  theme(legend.position="bottom", legend.direction="horizontal", 
        legend.title = element_blank())
库(ggplot2)
超滤