如何在ggplot2中创建一个线图框架

时间:2014-12-30 10:31:37

标签: r ggplot2 linegraph

基于下面的代码,我成功地在R中绘制了一个线条图。但我想在我的线条图周围画一个框架。

# To load the packages 
library(ggplot2)
library(reshape2)

# my data
dat<-structure(list(B50K = c(0.3795, 0.3178, 0.2795, 0.2417, 0.2002, 0.1642), 
B50K.1000C50K = c(0.4192, 0.3758, 0.3266, 0.2599, 0.2287, 0.1911), 
B50K.2000C50K = c(0.4675, 0.4249, 0.3763, 0.2898, 0.2531, 0.2151), 
B50K.4000C50K = c(0.5357, 0.501, 0.4636, 0.3291, 0.2962, 0.2544), 
B50K.8000C50K = c(0.6217, 0.587, 0.5583, 0.3736, 0.3485, 0.2951), 
TimestepTrait = structure(c(2L, 5L, 6L, 1L, 3L, 4L), 
.Label = c("T18-Fertility", "T18-Yield", "T19-Fertility", "T20-Fertility", "T19-Yield","T20-Yield"), 
class = "factor")), .Names = c("B-50K", "B-50K+1000 C-50K", "B-50K+2000 C-50K",
"B-50K+4000 C-50K", "B-50K+8000 C-50K", "TimestepTrait"), row.names = c(NA,
-6L), class = "data.frame")

# assume xaxile column is unneeded
dat$xaxile <- NULL

# reshape data for plotting
dat.m <- melt(dat)

dat.m$TimestepTrait <- factor(dat.m$TimestepTrait, levels=unique(dat$TimestepTrait), labels=unique(dat$TimestepTrait))

# plot
da<- ggplot(dat.m, aes(x=variable, y=value, colour=TimestepTrait, col= TimestepTrait,  
              shape=TimestepTrait, linetype=TimestepTrait, group=TimestepTrait)) +
              geom_point(size=4) + 
              geom_line(lwd=1.2) +
#                 theme_bw() +
#                 theme(panel.grid.major = element_line(colour = "#808080")) +
              theme(axis.text.x = element_text(angle = 00, hjust = 1, size=11, color="darkred")) +
              theme(axis.text.y = element_text(angle = 90, vjust = 1, size=13, color="darkred")) +
              theme(axis.title.y = element_text(size = rel(1.4), angle = 90)) +
              theme(axis.title.x = element_text(size = rel(1.2), angle = 00)) +
              theme(legend.text=element_text(size=14)) +
#                 theme(plot.legend = element_text(lineheight=3, face="bold", color="black", size=29)) +
              ylim(0.0, 0.7) +               
              scale_linetype_manual(values = c(rep("solid", 3), rep("dashed", 3)))
davk<-da + xlab("Reference Population Size") + ylab("Reliability (GEBV,TBV)")# + ggtitle("Davood")
ggsave("Figure 1.jpg", plot = davk, scale=2, width = 6, height = 4, dpi=500)              

顺便说一下,在R中运行下面的代码之后你可以看到,x和y轴的值偏离它们的正确位置。应该怎么解决呢?

任何帮助都会提前得到赞赏!

1 个答案:

答案 0 :(得分:2)

尝试

davk + theme(axis.text.x = element_text(hjust = .5), 
             axis.text.y = element_text(hjust = .5, vjust = .5),
             panel.border = element_rect(fill = NA))