Y轴上的刻度在geom_bar中未显示

huangapple go评论71阅读模式
英文:

Tics on y axis in geom_bar not showing up

问题

我有以下这样的代码:

data <- read.csv("test2.csv", stringsAsFactors=FALSE, header=TRUE)

# 绘图
myplot=ggplot(data, aes(fill=condition, y=value, x=condition)) + 
    geom_bar(position="dodge", stat="identity") +
    scale_fill_manual(values=c("#ffffb2", "#fecc5c"))+
    scale_y_discrete(breaks=c(10,20,30,40,50,60,70,80))+
    ylab("Performance (ns/day) on Extreme") +
    facet_wrap(~specie,nrow=3, labeller = label_wrap_gen(width = 85)) +
    theme_bw() +
    theme(panel.grid = element_blank(),
        panel.spacing = unit(0, "mm"),
        legend.title=element_blank(),
        axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))


myplot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.title=element_blank(), 
panel.background = element_blank(), axis.title.x = element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.title.y = element_text(angle = 90, hjust = 0.5, size = 20))

我的数据如下:

> dput(data)
structure(list(specie = c("gmx mdrun -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -s benchMEM.tpr -nsteps 10000", "gmx mdrun  -s benchPEP.tpr -nsteps 10000 ", 
"gmx mdrun  -s benchPEP.tpr -nsteps 10000 ", "gmx mdrun  -s benchPEP-h.tpr -nsteps 10000 ", 
"gmx mdrun  -s benchPEP-h.tpr -nsteps 10000 ", "gmx mdrun  -s MD_5NM_WATER.tpr -nsteps 10000 ", 
"gmx mdrun  -s MD_5NM_WATER.tpr -nsteps 10000 ", "gmx mdrun  -s data/MD_10NM_WATER.tpr -nsteps 10000 ", 
"gmx mdrun  -s data/MD_10NM_WATER.tpr -nsteps 10000 ", "gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000 ", 
"gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000 "), condition = c("CUDA", 
"SYCL", "CUDA", "SYCL", "CUDA", "SYCL", "CUDA", "SYCL", "CUDA", 
"SYCL", "CUDA", "SYCL"), value = c("54.682", "77.649", "0.673", 
"0.841", "0.86", "0.986", "452.48", "158 .277", "81.426", "80.454", 
"33.449", "34.363")), class = "data.frame", row.names = c(NA, 
-12L))

我通过以下方式设置了y轴的刻度:

scale_y_discrete(breaks=c(10,20,30,40,50,60,70,80))

但它们没有显示出来。请建议如何修复此问题。如果不包括scale_y_discrete,小数值将显示在y轴上,这不是我想要的。

英文:

I have code like this:

data &lt;- read.csv(&quot;test2.csv&quot;, stringsAsFactors=FALSE, header=TRUE)

# Graph
myplot=ggplot(data, aes(fill=condition, y=value, x=condition)) + 
    geom_bar(position=&quot;dodge&quot;, stat=&quot;identity&quot;) +
    scale_fill_manual(values=c(&quot;#ffffb2&quot;, &quot;#fecc5c&quot;))+
    scale_y_discrete(breaks=c(10,20,30,40,50,60,70,80))+
    ylab(&quot;Performance (ns/day) on Extreme&quot;) +
    facet_wrap(~specie,nrow=3, labeller = label_wrap_gen(width = 85)) +
   theme_bw() +
   theme(panel.grid = element_blank(),
        panel.spacing = unit(0, &quot;mm&quot;),
        legend.title=element_blank(),
        axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))


myplot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.title=element_blank(), 
panel.background = element_blank(), axis.title.x = element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.title.y = element_text(angle = 90, hjust = 0.5, size = 20))

My data is this:

&gt; dput(data)
structure(list(specie = c(&quot;gmx mdrun -s benchMEM.tpr -nsteps 10000&quot;, 
&quot;gmx mdrun -s benchMEM.tpr -nsteps 10000&quot;, &quot;gmx mdrun  -s benchPEP.tpr -nsteps 10000 &quot;, 
&quot;gmx mdrun  -s benchPEP.tpr -nsteps 10000 &quot;, &quot;gmx mdrun  -s benchPEP-h.tpr -nsteps 10000 &quot;, 
&quot;gmx mdrun  -s benchPEP-h.tpr -nsteps 10000 &quot;, &quot;gmx mdrun  -s MD_5NM_WATER.tpr -nsteps 10000 &quot;, 
&quot;gmx mdrun  -s MD_5NM_WATER.tpr -nsteps 10000 &quot;, &quot;gmx mdrun  -s data/MD_10NM_WATER.tpr -nsteps 10000 &quot;, 
&quot;gmx mdrun  -s data/MD_10NM_WATER.tpr -nsteps 10000 &quot;, &quot;gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000 &quot;, 
&quot;gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000 &quot;), condition = c(&quot;CUDA&quot;, 
&quot;SYCL&quot;, &quot;CUDA&quot;, &quot;SYCL&quot;, &quot;CUDA&quot;, &quot;SYCL&quot;, &quot;CUDA&quot;, &quot;SYCL&quot;, &quot;CUDA&quot;, 
&quot;SYCL&quot;, &quot;CUDA&quot;, &quot;SYCL&quot;), value = c(&quot;54.682&quot;, &quot;77.649&quot;, &quot;0.673&quot;, 
&quot;0.841&quot;, &quot;0.86&quot;, &quot;0.986&quot;, &quot;452.48&quot;, &quot;158 .277&quot;, &quot;81.426&quot;, &quot;80.454&quot;, 
&quot;33.449&quot;, &quot;34.363&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
-12L))

I did set ticks on y axis via:

scale_y_discrete(breaks=c(10,20,30,40,50,60,70,80))+

but they are not showing up. Please advise how to fix this. If I don't include scale_y_discrete decimal values are showing up on y axis which is not what I want.

答案1

得分: 0

除了像@Phil在评论中建议的使用as.numeric()之外,我还从一个值中删除了空格(158 .277)。如果在完整的数据框中引入了额外的NAs,value列在强制转换为数字之前可能需要一些清理。

将y轴数据集设置为数字后,您不一定需要自己调整刻度。在您的theme()中,您设置了axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),然后添加了axis.text.x=element_blank(),所以我在这些图中只保留了后者。

library(dplyr)
library(ggplot2)
myplot <- data %>%
  mutate(value = as.numeric(value)) %>%
  ggplot(aes(fill=condition, y=value, x=condition)) + 
  geom_bar(position="dodge", stat="identity") +
  scale_fill_manual(values=c("#ffffb2", "#fecc5c")) +
  ylab("Performance (ns/day) on Extreme") +
  facet_wrap(~specie, nrow=3, labeller = label_wrap_gen(width = 85)) +
  theme_bw() +
  theme(panel.grid = element_blank(),
        panel.spacing = unit(0, "mm"),
        legend.title=element_blank(),
        axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y = element_text(angle = 90, hjust = 0.5, size = 20))

myplot

如果您决定要控制y轴,可以使用scale_y_continuous()而不是discrete

myplot +
  scale_y_continuous(breaks = c(0, 50, 250, 400))
myplot +
  scale_y_continuous(breaks = scales::breaks_extended(n = 12))

<sup>创建于2023-06-26,使用 reprex v2.0.2</sup>

英文:

In addition to using as.numeric() as @Phil suggests in a comment, I removed whitespace from one of the values (158 .277). If you find additional NAs are introduced in the full data frame, the value column may require some clean up before coercing to numeric.

With the y-axis data set as a numeric, you don't necessarily need to adjust the scales yourself. In your theme() you set axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) and then add axis.text.x=element_blank(), so I have only kept the latter in these plots.

library(dplyr)

library(ggplot2)
myplot &lt;- data %&gt;%
  mutate(value = as.numeric(value)) %&gt;%
  ggplot(aes(fill=condition, y=value, x=condition)) + 
  geom_bar(position=&quot;dodge&quot;, stat=&quot;identity&quot;) +
  scale_fill_manual(values=c(&quot;#ffffb2&quot;, &quot;#fecc5c&quot;))+
  ylab(&quot;Performance (ns/day) on Extreme&quot;) +
  facet_wrap(~specie,nrow=3, labeller = label_wrap_gen(width = 85)) +
  theme_bw() +
  theme(panel.grid = element_blank(),
        panel.spacing = unit(0, &quot;mm&quot;),
        legend.title=element_blank(),
        axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y = element_text(angle = 90, hjust = 0.5, size = 20))

myplot

Y轴上的刻度在geom_bar中未显示<!-- -->

If you decide you do want to control the y-axis, you would use scale_y_continuous() instead of discrete.


myplot +
  scale_y_continuous(breaks = c(0,50,250,400))

Y轴上的刻度在geom_bar中未显示<!-- -->


myplot +
  scale_y_continuous(breaks = scales::breaks_extended(n = 12))

Y轴上的刻度在geom_bar中未显示<!-- -->

<sup>Created on 2023-06-26 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月26日 22:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定