[英]R: How Plot an Excel Table(Matrix) with R
I got this problem I still haven't found out how to solve it. 我遇到了这个问题,但仍然没有找到解决方法。 I want to plot all the Values
MW1
, MW2
and MW3
in function of "DHT + Procymidone". 我想在“ DHT + Procymidone”函数中绘制所有值
MW1
, MW2
和MW3
。 How can I plot all this values in the graphic so that I will get 3 different curves (in different colors and different number like curve 1, 2, ...)? 如何在图形中绘制所有这些值,以便获得3条不同的曲线(具有不同的颜色和不同的数字,例如曲线1、2 ...)? And I want the labels of the X-Values("DHT + Procymidone") to be like -10, -9, ... , -4 instead of 1,00E-10, ...
我希望X值的标签(“ DHT +脯氨酸”)像-10,-9,...,-4,而不是1,00E-10,...
DHT + Procymidone MW 1 MW 2 MW 3
1,00E-10 114,259526780335 111,022461066274 213,212408408682
1,00E-09 115,024187788314 111,083316791613 114,529425136628
1,00E-08 110,517449986348 107,867941606743 125,10230718665
1,00E-07 100,961311263444 98,4219995773135 116,045168653416
1,00E-06 71,2383604211297 73,539659636842 50,3213799775309
1,00E-05 20,3553333652104 36,1345771905088 15,42260866106
1,00E-04 4,06189509055904 18,1246447874679 10,1988107887318
I have shortened your data frame for convenience reasons, so here's an example: 为了方便起见,我缩短了您的数据框,因此下面是一个示例:
mydat <- data.frame(DHT_Procymidone = c(-10, -9, -8, -7, -6, -5, -4),
MW1 = c(114, 115, 110, 100, 72, 20, 4),
MW2 = c(111, 111, 107, 98, 73, 36, 18),
MW3 = c(213, 114, 123, 116, 50, 15, 10))
library(tidyr)
library(ggplot2)
mydf <- gather(mydat, "grp", "MW", 2:4)
ggplot(mydf, aes(x = DHT_Procymidone, y = MW, colour = grp)) + geom_line()
which gives following plot: 给出以下情节:
To use ggplot, your data needs to be in long-format. 要使用ggplot,您的数据必须为长格式。
gather
does this for you, appending columns MW1-MW3 into one column, while the column names are added as new column values in the grp
-column. gather
为您完成此,追加列MW1-MW3成一列,而列名添加为新的列值grp
-column。 This group-column allows to identify different groups, ie different colored lines in the plot. 该组列允许标识不同的组,即图中的不同彩色线。
Depending on the type of DHT + Procymidone
, you can, eg use format(..., scientific = FALSE)
to convert to numeric, however, this will result in -0.0000000001 (and not -10). 根据
DHT + Procymidone
的类型,您可以例如使用format(..., scientific = FALSE)
转换为数字,但是,这将导致-0.0000000001(而不是-10)。
However, if this data column is a character vector (you can coerce with as.character
), this may work: 但是,如果此数据列是字符向量(您可以使用
as.character
强制),则可以使用:
a <- "1,00E-10"
sub("1,00E", "", a, fixed = TRUE)
> [1] "-10"
As an alternative answer to @Daniel's which doesn't rely on ggplot (thanks Daniel for providing the reproducible data). 作为不依赖ggplot的@Daniel的替代答案(感谢Daniel提供了可复制的数据)。
mydat <- data.frame(DHT_Procymidone = c(-10, -9, -8, -7, -6, -5, -4),
MW1 = c(114, 115, 110, 100, 72, 20, 4),
MW2 = c(111, 111, 107, 98, 73, 36, 18),
MW3 = c(213, 114, 123, 116, 50, 15, 10))
plot(mydat[,2] ~ mydat[,1], typ = "l", ylim = c(0,220), xlim = c(-10,-2), xlab = "DHT Procymidone", ylab = "MW")
lines(mydat[,3] ~ mydat[,1], col = "blue")
lines(mydat[,4] ~ mydat[,1], col = "red")
legend(x = -4, y = 200, legend = c("MW1","MW2","MW3"), lty = 1, bty = "n", col = c("black","blue","red"))
To change axis labels see the text in xlab
and ylab
. 要更改轴标签,请参见
xlab
和ylab
的文本。 To change axis limits see xlim
and ylim
. 要更改轴限制,请参见
xlim
和ylim
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.