这是我想要绘制的数据:
structure(list(`10` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
`34` = c(0, 0, 0, 0, 0, 0, 0, 0, 547725, 0),
`59` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
`84` = c(0, 0, 0, 8173070.8, 0, 0, 0, 0, 0, 0),
`110` = c(0, 0, 0, 20302893.6, 0, 0, 0, 0, 0, 0),
`134` = c(0, 0, 0, 13696077.5, 0, 0, 0, 0, 0, 0),
`165` = c(1024325, 0, 0, 10486165.5, 0, 0, 0, 0, 0, 0),
`199` = c(1183267.5, 0, 0, 6015700, 0, 0, 0, 0, 0, 0),
`234` = c(1771708.3, 0, 0, 3384495.8, 3384495.8, 0, 0, 0, 0, 1144700),
`257` = c(2007712.3, 0, 0, 0, 6980230.6, 0, 0, 0, 0, 0),
`362` = c(3339118.9, 0, 0, 0, 7280030.6, 1119625, 0, 0, 0, 0),
`433` = c(973797.9, 0, 0, 0, 6230170, 1497625, 0, 0, 0, 0),
`506` = c(0, 0, 0, 0, 12905925, 0, 0, 0, 0, 0),
`581` = c(0, 2140050, 0, 0, 4560645.8, 0, 3170133.3, 0, 0, 0),
`652` = c(0, 639437.7, 639437.7, 0, 2349711.3, 0, 902318.3, 902318.3, 0, 0),
`733` = c(0, 0, 1397257.5, 0, 2274710, 0, 0, 1414458.3, 0, 0),
`818` = c(0, 0, 742731.8, 0, 2953550, 0, 0, 563876.7, 0, 0),
`896` = c(0, 0, 714654.7, 0, 1199563.3, 0, 0, 561000, 0, 0),
`972` = c(0, 0, 434271.5, 0, 1358225, 0, 0, 0, 0, 0),
`1039` = c(0, 0, 227435, 0, 934840, 0, 0, 0, 0, 0)),
.Names = c("10", "34", "59", "84", "110", "134", "165", "199", "234", "257", "362", "433", "506", "581", "652", "733", "818", "896", "972", "1039"),
row.names = c("Mark121_1", "Mark121_2", "Mark121_3", "Mark143_1", "Mark143_2", "Mark152_1", "Mark152_2", "Mark152_3", "Mark444_1", "Mark444_2"),
class = "data.frame")
我想在同一个地块上为_
(破折号)之后的数字添加不同行的行。线条的不同颜色是必要的。我在考虑使用matplot
,但我不知道如何选择具有相似字符串的行。
使用简单的单词,我希望有以下几行:
在同一图表上。这意味着包含多行的4个不同图表。
答案 0 :(得分:2)
此解决方案使用" dplyr"和" ggplot2"和" purrr"。规模存在很大差异,因此我更改为log10,您可能不希望这样。
df2 <- df %>% mutate(Name= rownames(.)) %>%
gather(key=period, value=value,-Name) %>%
mutate(person= sub("_.", "", Name), period=as.numeric(period))
df2 %>% ggplot(., aes(x=period, y=log10(value), colour=Name, group=Name)) +
geom_line() + facet_wrap(~person)
修改:其他请求
为了单独绘制每个数字
#This saves the figures as a list of plot objects
FiguresList <- unique(df2$person) %>% map(function(P) {
df2 %>% filter(person ==P) %>%
ggplot(., aes(x=period, y=log10(value), colour=Name, group=Name)) +
geom_line()}
)
FiguresList[[1]]
#This saves each plot as a pdf named by the person e.g "Mark121.pdf"
unique(df2$person) %>% map(function(P) {
df2 %>% filter(person ==P) %>%
ggplot(., aes(x=period, y=log10(value), colour=Name, group=Name)) +
geom_line()
ggsave(paste(P,".pdf", sep=""))}
)