Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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中抑制图形中的数据_R_Ggplot2 - Fatal编程技术网

在R中抑制图形中的数据

在R中抑制图形中的数据,r,ggplot2,R,Ggplot2,我有一个数据集d,它包含个人可识别的数据,我有一个数据集为所有被抑制的值放一个X: column1 column2 column3 * FSM X * Male 2.5 * Female X A FSM 6 A Male 10.3 A Female 11.7 B

我有一个数据集d,它包含个人可识别的数据,我有一个数据集为所有被抑制的值放一个X:

column1     column2     column3
*           FSM         X
*           Male        2.5
*           Female      X
A           FSM         6
A           Male        10.3
A           Female      11.7
B           FSM         14.8
B           Male        21.5
B           Female      25.3
我想在条形图中,在条形图的上方用X来绘制,其中数据已被抑制,例如:

我的代码是:

p <- ggplot(d, aes(x=column1, y=column3, fill=column2)) + 
  geom_bar(position=position_dodge(), stat="identity", colour="black") +
  geom_text(aes(label=column2),position= position_dodge(width=0.9), vjust=-.5) 
  scale_y_continuous("Percentage",breaks=seq(0, max(d$column3), 2)))
如何使条形图忽略“X”并仍然添加标签(如果存在)

数据转储:

structure(list(column1 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L), .Label = c("*", 
"A", "B", "C", "D", "E", "U"), class = "factor"), column2 = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L), .Label = c("FSM", "Male", "Female"), class = "factor"), 
    column3 = structure(c(21L, 1L, 2L, 18L, 3L, 4L, 7L, 12L, 
    14L, 16L, 15L, 13L, 10L, 9L, 8L, 11L, 6L, 5L, 20L, 19L, 17L
    ), .Label = c("1.93889541715629", "1.97444831591173", "10.1057579318449", 
    "11.7305458768873", "12.7758420441347", "14.4535840188014", 
    "14.8471615720524", "18.5830429732869", "19.9764982373678", 
    "20.0873362445415", "20.9606986899563", "21.5628672150411", 
    "24.1579558652729", "25.3193960511034", "25.7931844888367", 
    "29.2576419213974", "5.45876887340302", "6.11353711790393", 
    "6.16921269095182", "6.98689956331878", "X"), class = "factor")), .Names = c("column1", 
"column2", "column3"), row.names = c(NA, -21L), class = "data.frame")

我很乐意在有0个实例的情况下打印出0个实例,但是在数据抑制的情况下,我想通过打印一个“X”来明确数据已被抑制,但该条也将显示0个实例,首先将高度转换为数值,这为删失值提供了NA。然后在此基础上创建一个标签列。然后需要一列零作为标签的y坐标

> d$column3=as.numeric(as.character(d$column3))
Warning message:
NAs introduced by coercion 
> d$column4 = ifelse(is.na(d$column3),"X","")
> d$y=0
然后:

p p+geom\u条(position=position\u dodge(),stat=“identity”, color=“黑色”)+ 几何图形文本(aes(标签=第4列,x=第1列,y=y), 位置=位置\减淡(宽度=1),vjust=-0.5) 给予:


它是一种用条形图的值标记几何图形条形图的变体。几乎是一个重复。

首先将高度转换为数值,这将为删失值提供NA。然后在此基础上创建一个标签列。然后需要一列零作为标签的y坐标

> d$column3=as.numeric(as.character(d$column3))
Warning message:
NAs introduced by coercion 
> d$column4 = ifelse(is.na(d$column3),"X","")
> d$y=0
然后:

p p+geom\u条(position=position\u dodge(),stat=“identity”, color=“黑色”)+ 几何图形文本(aes(标签=第4列,x=第1列,y=y), 位置=位置\减淡(宽度=1),vjust=-0.5) 给予:

它是一种用条形图的值标记几何图形条形图的变体。几乎是重复。

您能给我们一个转储(使用dput)您的样本数据吗?你想要的图形应该很简单,有几个层,但我不想费心构建这样一个精细的数据集…你能给我们一个转储(使用dput)你的样本数据吗?你想要的图形应该很简单,有几个层,但我不想费心构建这样一个精细的数据集。。。
> p <- ggplot(d, aes(x=column1, y=column3, fill=column2))
> p + geom_bar(position=position_dodge(), stat="identity",
   colour="black") + 
 geom_text(aes(label=column4,x=column1,y=y),
   position=position_dodge(width=1), vjust=-0.5)