[英]How to plot 2 lines in same graphs in R ggplot2 with small values
我有以下CSV格式的數據
0.828666667 0.100333333
0.725666667 0.153666667
0.364333333 0.036666667
0.475666667 0.051
0.522333333 0.052333333
0.457 0.041666667
0.644666667 0.093333333
0.404333333 0.039333333
0.497 0.042333333
0.155666667 0.031666667
0.160666667 0.081333333
0.145666667 0.026666667
0.138666667 0.033666667
0.094333333 0.03
0.141 0.023666667
0.148666667 0.052
0.195666667 0.039
0.196333333 0.039333333
......
我正在使用以下代碼
library(ggplot2)
data<-read.csv("sample.csv",header=TRUE,sep=",")
ggplot(data,aes(x=A,y=B,))+ geom_line() + scale_x_continuous(breaks=seq(0,9,0.5)) +
scale_y_continuous(breaks=seq(0,9,0.5))
我想在x和y軸上使用相同的比例尺,這就是我正在使用breaks ..但這並不能給出我想要的效果
但是我想成像如下
感謝幫助
我不確定我確切了解您想要做什么。 但是,這是我嘗試從您的數據中獲取與您的第二幅圖相似的東西。
data <- read.table(text="0.828666667 0.100333333
0.725666667 0.153666667
0.364333333 0.036666667
0.475666667 0.051
0.522333333 0.052333333
0.457 0.041666667
0.644666667 0.093333333
0.404333333 0.039333333
0.497 0.042333333
0.155666667 0.031666667
0.160666667 0.081333333
0.145666667 0.026666667
0.138666667 0.033666667
0.094333333 0.03
0.141 0.023666667
0.148666667 0.052
0.195666667 0.039
0.196333333 0.039333333")
names(data) <- c("A", "B")
# prepare data for plotting
require(reshape2)
data$id <- 1:nrow(data)
df <- melt(data, id.var="id")
# plot
library(ggplot2)
ggplot(df, aes(x=id, y=value, color=variable)) + geom_line()
如果這不能回答您的問題,請嘗試更具體。
我認為您需要對數據進行一些操作...
library(reshape2)
library(ggplot2)
dat <- YOUR CSV
names(dat) <- c('a', 'b')
# need an x for the plot
dat$Num <- as.numeric(row.names(dat))
meltDat <- melt(dat, id.vars = 'Num')
ggplot(meltDat,
aes(x = Num, y = value, group = variable, color = variable)) +
geom_line()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.