簡體   English   中英

如何在ggplot2中創建線圖框架

[英]how to create a line plot frame in ggplot2

根據下面的代碼,我成功地在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軸的值偏離了它們的正確位置。 應該怎么解決呢?

任何幫助將不勝感激!

嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM