简体   繁体   English

使用R ggplot2的简单线条图

[英]Simple line plot using R ggplot2

I have data as follows in .csv format as I am new to ggplot2 graphs I am not able to do this 我有.csv格式的以下数据,因为我是ggplot2图的新手,我无法执行此操作

T           L
141.5453333 1
148.7116667 1
154.7373333 1
228.2396667 1
148.4423333 1
131.3893333 1
139.2673333 1
140.5556667 2
143.719     2
214.3326667 2
134.4513333 3
169.309     8
161.1313333 4

I tried to plot a line graph using following graph 我试图使用以下图形绘制折线图

data<-read.csv("sample.csv",head=TRUE,sep=",")

ggplot(data,aes(T,L))+geom_line()]

but I got following image it is not I want 但是我得到了下面的图像,这不是我想要的

在此处输入图片说明

I want following image as follows 我想要以下图像

在此处输入图片说明

Can anybody help me? 有谁能够帮助我?

You want to use a variable for the x-axis that has lots of duplicated values and expect the software to guess that the order you want those points plotted is given by the order they appear in the data set. 您想为x轴使用一个变量,该变量具有很多重复的值,并且希望软件猜测您要绘制这些点的顺序是由它们在数据集中显示的顺序给出的。 This also means the values of the variable for the x-axis no longer correspond to the actual coordinates in the coordinate system you're plotting in, ie, you want to map a value of "L=1" to different locations on the x-axis depending on where it appears in your data. 这也意味着x轴的变量值不再对应于您正在绘制的坐标系中的实际坐标,即,您希望将值“ L = 1”映射到x上的不同位置-轴,取决于它在数据中的显示位置。

This type of fairly non-sensical thing does not work in ggplot2 out of the box. 这种类型的非常荒谬的事情在ggplot2ggplot2 You have to define a separate variable that has a proper mapping to values on the x-axis ("id" in the code below) and then overwrite the labels with the values for "L". 您必须定义一个单独的变量,该变量必须与x轴上的值正确映射(以下代码中的“ id”),然后用“ L”的值覆盖标签。

The coe below shows you how to do this, but it seems like a different graphical display would probbaly be better suited for this kind of data. 下面的coe向您展示了如何执行此操作,但似乎不同的图形显示可能更适合此类数据。

data <- as.data.frame(matrix(scan(text="
141.5453333 1
148.7116667 1
154.7373333 1
228.2396667 1
148.4423333 1
131.3893333 1
139.2673333 1
140.5556667 2
143.719     2
214.3326667 2
134.4513333 3
169.309     8
161.1313333 4
"), ncol=2, byrow=TRUE))
names(data) <- c("T", "L")
data$id <- 1:nrow(data)
ggplot(data,aes(x=id, y=T))+geom_line() + xlab("L") +
    scale_x_continuous(breaks=data$id, labels=data$L)

在此处输入图片说明

You have an error in your code, try this: 您的代码中有错误,请尝试以下操作:

ggplot(data,aes(x=L, y=T))+geom_line()

Default arguments for aes are: aes默认参数为:

aes(x, y, ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM