Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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 2x2条形图中以X轴45度显示所有标签_R_Plot_Bar Chart - Fatal编程技术网

如何在R 2x2条形图中以X轴45度显示所有标签

如何在R 2x2条形图中以X轴45度显示所有标签,r,plot,bar-chart,R,Plot,Bar Chart,使用以下数据: Method Metric E0 E1 E2 E4 Method-XXX Precision 0.9661017 0.9622642 1 0.9655172 Method-YYY Precision 0.533 0.535 0.378 0.214 Method-ZZZ Precision 0.595 0.843 0.77 0.689 Method-XZZZ Precision 0.573 0.698 0

使用以下数据:

Method  Metric  E0  E1  E2  E4
Method-XXX  Precision   0.9661017   0.9622642   1   0.9655172
Method-YYY  Precision   0.533   0.535   0.378   0.214
Method-ZZZ  Precision  0.595    0.843   0.77    0.689
Method-XZZZ Precision   0.573   0.698   0.53    0.708
Method-XZZZY    Precision   0.008   0.011   0.004   0.002
Method-XXX  Recall  0.9736842   0.9736842   0.9473684   0.9473684
Method-YYY     Recall   1   1   1   0.667
Method-ZZZ  Recall       0.833  1   1   1
Method-XZZZ Recall  1   1   1   1
Method-XZZZY    Recall  0.167   0.75    1   1
我可以创建此绘图:

但是,正如您所看到的,x轴并非都指定了标签。 我怎样才能做到这一点? 如果我们将x轴旋转45度也可以。但我不知道该怎么做:

这是我的代码(由提供):

dat添加以旋转45度
添加以旋转45度

这里有一种不用旋转就能得到所有标签的方法。将轴标签打印在两条线上,而不是一条线上,以避免重叠。我用一个图表来演示这个方法

dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)

# Create barplot
barplot(height=dat$E0, beside=TRUE, col=c("red","blue"))

# Get x-coordinates of bars
x.coords = barplot(height=dat$E0, beside=TRUE, plot=FALSE)
# Create new coordinates between each pair of bars
new.x.coords = seq(sum(x.coords)[1:2]/2, sum(x.coords)[9:10]/2, x.coords[2]-x.coords[1])

# Plot axis labels, but not axis or tickmarks
axis(side=1, at=new.x.coords[c(1,3,5)], labels=dat$Method[c(1,3,5)], line=0, tick=FALSE)
axis(side=1, at=new.x.coords[c(2,4)], labels=dat$Method[c(2,4)], line=1, tick=FALSE)
# Plot just axis and tickmarks, but not labels
axis(side=1, at=new.x.coords, labels=NA)

dat这里有一种不用旋转就能获取所有标签的方法。将轴标签打印在两条线上,而不是一条线上,以避免重叠。我用一个图表来演示这个方法

dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)

# Create barplot
barplot(height=dat$E0, beside=TRUE, col=c("red","blue"))

# Get x-coordinates of bars
x.coords = barplot(height=dat$E0, beside=TRUE, plot=FALSE)
# Create new coordinates between each pair of bars
new.x.coords = seq(sum(x.coords)[1:2]/2, sum(x.coords)[9:10]/2, x.coords[2]-x.coords[1])

# Plot axis labels, but not axis or tickmarks
axis(side=1, at=new.x.coords[c(1,3,5)], labels=dat$Method[c(1,3,5)], line=0, tick=FALSE)
axis(side=1, at=new.x.coords[c(2,4)], labels=dat$Method[c(2,4)], line=1, tick=FALSE)
# Plot just axis and tickmarks, but not labels
axis(side=1, at=new.x.coords, labels=NA)

dat使用
restrape2
(用于将数据重塑为长格式)和
ggplot2
(用于打印)软件包,制作这样的打印会容易得多

守则:

dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)

library(reshape2)
library(ggplot2)

# reshape your data into long format
long <- melt(dat, id=c("Method","Metric"), 
             measure=c("E0","E1","E2","E4"),
             variable = "E.nr")

# make the plot
ggplot(long) +
  geom_bar(aes(x = Method, y = value, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  facet_wrap(~E.nr) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=45, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14),
    strip.background = element_rect(color="white", fill="white"),
    strip.text = element_text(size=16)
  )
dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)

library(ggplot2)
library(gridExtra)

# making the seperate plots 
pE0 <- ggplot(dat) +
  geom_bar(aes(x = Method, y = E0, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(title="E0\n",x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=30, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14)
  )

pE1 <- ggplot(dat) +
  geom_bar(aes(x = Method, y = E1, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(title="E1\n",x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=30, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14)
  )

pE2 <- ggplot(dat) +
  geom_bar(aes(x = Method, y = E2, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(title="E2\n",x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=30, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14)
  )

pE4 <- ggplot(dat) +
  geom_bar(aes(x = Method, y = E4, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(title="E4\n",x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=30, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14)
  )

# function to extract the legend (borrowed from: https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs )
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

legend <- g_legend(pE1)
lwidth <- sum(legend$width)

# combining the plots with gridExtra
grid.arrange(arrangeGrob(pE0 + theme(legend.position="none"),
                         pE1 + theme(legend.position="none"),
                         pE2 + theme(legend.position="none"),
                         pE4 + theme(legend.position="none")
                         ), 
             legend, widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
结果是:

使用
restrape2
(用于将数据重塑为长格式)和
ggplot2
(用于打印)软件包,制作这样的打印会容易得多

守则:

dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)

library(reshape2)
library(ggplot2)

# reshape your data into long format
long <- melt(dat, id=c("Method","Metric"), 
             measure=c("E0","E1","E2","E4"),
             variable = "E.nr")

# make the plot
ggplot(long) +
  geom_bar(aes(x = Method, y = value, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  facet_wrap(~E.nr) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=45, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14),
    strip.background = element_rect(color="white", fill="white"),
    strip.text = element_text(size=16)
  )
dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)

library(ggplot2)
library(gridExtra)

# making the seperate plots 
pE0 <- ggplot(dat) +
  geom_bar(aes(x = Method, y = E0, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(title="E0\n",x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=30, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14)
  )

pE1 <- ggplot(dat) +
  geom_bar(aes(x = Method, y = E1, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(title="E1\n",x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=30, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14)
  )

pE2 <- ggplot(dat) +
  geom_bar(aes(x = Method, y = E2, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(title="E2\n",x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=30, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14)
  )

pE4 <- ggplot(dat) +
  geom_bar(aes(x = Method, y = E4, fill = Metric), 
           stat="identity", position = "dodge", width = 0.7) +
  scale_fill_manual("Metric\n", values = c("red","blue"), 
                    labels = c(" Precision", " Recall")) +
  labs(title="E4\n",x="",y="") +
  theme_bw() +
  theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=16),
    axis.text.x = element_text(size=14, angle=30, hjust=1, vjust=1),
    axis.title.y = element_text(size=16, angle = 90),
    axis.text.y = element_text(size=14)
  )

# function to extract the legend (borrowed from: https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs )
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

legend <- g_legend(pE1)
lwidth <- sum(legend$width)

# combining the plots with gridExtra
grid.arrange(arrangeGrob(pE0 + theme(legend.position="none"),
                         pE1 + theme(legend.position="none"),
                         pE2 + theme(legend.position="none"),
                         pE4 + theme(legend.position="none")
                         ), 
             legend, widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
结果是:

遵循中使用的(和中第一个示例中演示的)基本相同的策略,您可以使用
grid.text()
注释基本图形输出

library(gridBase)

## Function that plots barplots with x-axes annotated with slanted
ff <- function(x) {
    barcols <- c("red","blue")

    ## Plot, suppressing the labels
    bp <- barplot(matrix(dat[,x], nrow = 2, byrow = TRUE), xaxt = "n",
                  beside = TRUE, col = barcols)
    title(main=names(dat[x]))
    xaxislab <- c("Method-XXX", "Method-YYY", " Method-ZZZ",
                  "Method-XZZZ", " Method-XZZZY")

    ## Compute x-axis coordinate at center of each group
    bp <- colMeans(bp) 

    ## Use gridBase to compute viewport coordinates and
    ## grid to push/pop viewports and add the labels
    vps <- baseViewports()
    pushViewport(vps$inner, vps$figure, vps$plot)
    grid.text(xaxislab,
        x = unit(bp, "native"), y = unit(-0.5, "lines"),
        just = "right", rot = 45, gp=gpar(cex=0.7))
    popViewport(3)
}

## Apply it to your data 
dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
sapply(3:6, ff)
库(gridBase)
##用于打印带有x轴的条形图的函数,x轴带有倾斜注释

ff遵循中使用的基本相同策略(在中的第一个示例中演示),您可以使用
grid.text()
基本图形输出进行注释

library(gridBase)

## Function that plots barplots with x-axes annotated with slanted
ff <- function(x) {
    barcols <- c("red","blue")

    ## Plot, suppressing the labels
    bp <- barplot(matrix(dat[,x], nrow = 2, byrow = TRUE), xaxt = "n",
                  beside = TRUE, col = barcols)
    title(main=names(dat[x]))
    xaxislab <- c("Method-XXX", "Method-YYY", " Method-ZZZ",
                  "Method-XZZZ", " Method-XZZZY")

    ## Compute x-axis coordinate at center of each group
    bp <- colMeans(bp) 

    ## Use gridBase to compute viewport coordinates and
    ## grid to push/pop viewports and add the labels
    vps <- baseViewports()
    pushViewport(vps$inner, vps$figure, vps$plot)
    grid.text(xaxislab,
        x = unit(bp, "native"), y = unit(-0.5, "lines"),
        just = "right", rot = 45, gp=gpar(cex=0.7))
    popViewport(3)
}

## Apply it to your data 
dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
sapply(3:6, ff)
库(gridBase)
##用于打印带有x轴的条形图的函数,x轴带有倾斜注释

谢谢,但是旋转180度太强了。我希望它最多是45度。对于45度,你可以检查我已经为45度做了更新…希望它会有帮助谢谢,但是旋转太强了180度。我希望它最多是45度。对于45度,你可以检查我已经为45度做了更新…希望它会有所帮助