英文:
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 <- read.csv("test2.csv", stringsAsFactors=FALSE, header=TRUE)
# Graph
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))
My data is this:
> 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))
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 <- 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
<!-- -->
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))
<!-- -->
myplot +
scale_y_continuous(breaks = scales::breaks_extended(n = 12))
<!-- -->
<sup>Created on 2023-06-26 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论