R 使用ggplot将y轴添加到截断条形图
我有以下数据(4列6行数据): 我提出了这个R脚本,它使用ggplot生成了一个漂亮的截断条形图:R 使用ggplot将y轴添加到截断条形图,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我有以下数据(4列6行数据): 我提出了这个R脚本,它使用ggplot生成了一个漂亮的截断条形图: ## importing data plot <- read.delim("data", row.names=1) View(plot) ### loading required packages if (!require("Rcpp")) { install.packages("Rcpp", repos="http://cran.rstudio.com/") library
## importing data
plot <- read.delim("data", row.names=1)
View(plot)
### loading required packages
if (!require("Rcpp")) {
install.packages("Rcpp", repos="http://cran.rstudio.com/")
library("Rcpp")
}
library("Rcpp")
if (!require("ggplot2")) {
install.packages("ggplot2", repos="http://cran.rstudio.com/")
library("ggplot2")
}
library("ggplot2")
if (!require("grid")) {
install.packages("grid", repos="http://cran.rstudio.com/")
library("grid")
}
library("grid")
if (!require("reshape2")) {
install.packages("reshape2", repos="http://cran.rstudio.com/")
library("reshape2")
}
library(reshape2)
## transpose "plot"
plot = t(plot)
plot <- melt(plot, id.vars=2:ncol(plot))
## running ggplot
bars = ggplot(plot[!is.na(plot$value),], aes(x = Var2, y = value, fill = factor(Var1))) +
geom_bar(stat = "identity") +
scale_fill_manual(values=rainbow(length(levels(plot$Var1)))) + facet_grid(Var1~., as.table=FALSE, scale="free_y", space = "free_y") + theme_minimal() +
geom_vline(xintercept=max(as.numeric(plot$Var2))+ 0.586, size=0.3) +
theme(legend.position="none",
axis.text.x = element_text(angle = 90, colour="black", vjust = 0.4, size=8),
axis.title.x = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(),
axis.line.y=element_blank(), axis.text.y=element_blank(),
strip.text.y=element_text(size = 8, colour="black", family="",angle=00, hjust = 0),
panel.grid=element_blank(),
axis.line=element_line(size = 0.3, colour = "black", linetype = "solid"),
axis.ticks.x=element_line(size = 0.3, colour = "black", linetype = "solid"),
panel.background=element_blank(), panel.margin = unit(0, "lines"))
bars
## exporting barplot "plot.png" to directory
#loading Cairo
if (!require("Cairo")) {
install.packages("Cairo", repos="http://cran.rstudio.com/")
library("Cairo")
}
library("Cairo")
#preparing exporting
png(file="plot.png",type="cairo", width = 4, height = 4.2, units = 'in',pointsize=8,res=600)
#exporting
barplot ; dev.off()
导入数据
plot您使用以下选项明确关闭了面板的y轴:
主题(…,axis.title.y=element\u blank(),axis.ticks.y=element\u blank(),axis.line.y=element\u blank(),axis.text.y=element\u blank(),…)
删除这些线条,你的身材就会如你所愿出现。天哪,你说得对,我一定是出于某种原因删除了它们,然后就忘了。。。谢谢你注意到了!
## importing data
plot <- read.delim("data", row.names=1)
View(plot)
### loading required packages
if (!require("Rcpp")) {
install.packages("Rcpp", repos="http://cran.rstudio.com/")
library("Rcpp")
}
library("Rcpp")
if (!require("ggplot2")) {
install.packages("ggplot2", repos="http://cran.rstudio.com/")
library("ggplot2")
}
library("ggplot2")
if (!require("grid")) {
install.packages("grid", repos="http://cran.rstudio.com/")
library("grid")
}
library("grid")
if (!require("reshape2")) {
install.packages("reshape2", repos="http://cran.rstudio.com/")
library("reshape2")
}
library(reshape2)
## transpose "plot"
plot = t(plot)
plot <- melt(plot, id.vars=2:ncol(plot))
## running ggplot
bars = ggplot(plot[!is.na(plot$value),], aes(x = Var2, y = value, fill = factor(Var1))) +
geom_bar(stat = "identity") +
scale_fill_manual(values=rainbow(length(levels(plot$Var1)))) + facet_grid(Var1~., as.table=FALSE, scale="free_y", space = "free_y") + theme_minimal() +
geom_vline(xintercept=max(as.numeric(plot$Var2))+ 0.586, size=0.3) +
theme(legend.position="none",
axis.text.x = element_text(angle = 90, colour="black", vjust = 0.4, size=8),
axis.title.x = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(),
axis.line.y=element_blank(), axis.text.y=element_blank(),
strip.text.y=element_text(size = 8, colour="black", family="",angle=00, hjust = 0),
panel.grid=element_blank(),
axis.line=element_line(size = 0.3, colour = "black", linetype = "solid"),
axis.ticks.x=element_line(size = 0.3, colour = "black", linetype = "solid"),
panel.background=element_blank(), panel.margin = unit(0, "lines"))
bars
## exporting barplot "plot.png" to directory
#loading Cairo
if (!require("Cairo")) {
install.packages("Cairo", repos="http://cran.rstudio.com/")
library("Cairo")
}
library("Cairo")
#preparing exporting
png(file="plot.png",type="cairo", width = 4, height = 4.2, units = 'in',pointsize=8,res=600)
#exporting
barplot ; dev.off()