I have a data.frame
我有一个data.frame
> head(df,30)
method FP TP data
1 HF 0 1 A p=10000 n=200 SNR=0.5
2 HM 15 2 A p=10000 n=200 SNR=0.5
3 HMP80 7 2 A p=10000 n=200 SNR=0.5
4 HMP90 2 2 A p=10000 n=200 SNR=0.5
5 LCV 0 2 A p=10000 n=200 SNR=0.5
6 LKSC 0 1 A p=10000 n=200 SNR=0.5
7 LP70 0 1 A p=10000 n=200 SNR=0.5
8 LP85 0 1 A p=10000 n=200 SNR=0.5
9 SS0206 0 0 A p=10000 n=200 SNR=0.5
10 SS0208 0 0 A p=10000 n=200 SNR=0.5
11 SS0406 0 0 A p=10000 n=200 SNR=0.5
12 SS0408 0 0 A p=10000 n=200 SNR=0.5
13 SS0506 0 0 A p=10000 n=200 SNR=0.5
14 SS0508 0 0 A p=10000 n=200 SNR=0.5
15 HF 26 7 A p=10000 n=200 SNR=2
16 HM 137 9 A p=10000 n=200 SNR=2
17 HMP80 136 9 A p=10000 n=200 SNR=2
18 HMP90 64 8 A p=10000 n=200 SNR=2
19 LCV 67 8 A p=10000 n=200 SNR=2
20 LKSC 0 3 A p=10000 n=200 SNR=2
21 LP70 67 8 A p=10000 n=200 SNR=2
22 LP85 51 8 A p=10000 n=200 SNR=2
23 SS0206 0 2 A p=10000 n=200 SNR=2
24 SS0208 0 0 A p=10000 n=200 SNR=2
25 SS0406 0 0 A p=10000 n=200 SNR=2
26 SS0408 0 0 A p=10000 n=200 SNR=2
27 SS0506 0 0 A p=10000 n=200 SNR=2
28 SS0508 0 0 A p=10000 n=200 SNR=2
29 HF 66 16 A p=10000 n=200 SNR=10
30 HM 292 16 A p=10000 n=200 SNR=10
That I am plotting with ggplot2
我正在用ggplot2进行策划
d = melt(df, id=c("method", "data"))
ggplot() + geom_bar(data=d, aes(x=method, y=value, fill=variable), stat='identity', position='dodge', width=0.5) +
facet_wrap(~data, ncol=3) +
theme_bw() + xlab("Method") + ylab("") +
theme(axis.title.x = element_text(face="bold", colour="#990000", size=20),
axis.text.x = element_text(angle=90, vjust=0.5, size=20),
axis.text.y = element_text(angle=0, vjust=0.5, size=20),
legend.text = element_text(colour = 'black', angle = 0, size = 20),
legend.title = element_text(colour = 'white', angle = 0, size = 20, hjust = 0, vjust = 0))
There are 2 things that I would like to change:
我想改变两件事:
1 - when I plot the order of the faced is defined by the value 'data'. A string with SNR=10 comes before the string with SNR=2. I would like to have the data with SNR=2 before the one with SNR=10
1 - 当我绘制面部的顺序时,由值'data'定义。 SNR = 10的字符串出现在SNR = 2的字符串之前。我想在SNR = 10之前获得SNR = 2的数据
2- I would like a different scale on each facet. (Now in each facet y goes from 0 to almost 300. )
2-我希望每个方面都有不同的比例。 (现在每个方面都从0变为近300.)
How can I change this?
我怎么能改变这个?
EDIT:
编辑:
I have found a solution to the first problem
我找到了第一个问题的解决方案
u = unique(df$data)
levels(u) = u
d$data = factor(df$data, levels=levels(u))
Regarding the second problem I am using the option
关于我使用该选项的第二个问题
facet_wrap(~data, ncol=3, scales = 'free_y')
But the problem is that I would like only integer values on the axis. I have tried scale_y_discrete()
but in this case too many values are plotted in some facets. How can I solve this?
但问题是我想在轴上只有整数值。我尝试过scale_y_discrete(),但在这种情况下,在某些方面绘制了太多的值。我怎么解决这个问题?
EDIT :
编辑:
This is my current R code. I have tried the scales packages but I do not understand how to use it in my situation.
这是我目前的R代码。我已经尝试过秤包但我不明白如何在我的情况下使用它。
df = read.csv(file = "Desktop/n200_p10000.csv")
df$method.1 = NULL
df$score = NULL
df$FN = NULL
df$TN = NULL
d = melt(df, id=c("method", "data"))
u = unique(df$data)
levels(u) = u
d$data = factor(df$data, levels=levels(u))
ggplot() + geom_bar(data=d, aes(x=method, y=value, fill=variable), stat='identity', position='dodge', width=0.5) +
facet_wrap(~data, ncol=3, scales = 'free_y') +
scale_y_continuous() +
theme_bw() + xlab("Method") + ylab("") +
theme(axis.title.x = element_text(face="bold", colour="#990000", size=20),
axis.text.x = element_text(angle=90, vjust=0.5, size=20),
axis.text.y = element_text(angle=0, vjust=0.5, size=20),
legend.text = element_text(colour = 'black', angle = 0, size = 20),
legend.title = element_text(colour = 'white', angle = 0, size = 20, hjust = 0, vjust = 0))
1 个解决方案
#1
1
To manipulate the order of facets, you need the levels of the factor you are faceting on to be sorted as you want. So you need to order the levels of data
. This can be done for example like df$data <- factor(df$data, levels = c(...))
(with your desired order of levels in c()
).
要操纵构面的顺序,您需要根据需要对要面对的因子的级别进行排序。所以你需要订购数据级别。这可以例如像df $ data < - factor(df $ data,levels = c(...))(在c()中具有所需的级别顺序)。
Regarding your second question, look at ?facet_wrap
and in particular the scales
option. You will have to set scales = 'free'
or 'free_y'
.
关于你的第二个问题,请看?facet_wrap,特别是scale选项。您必须设置scales ='free'或'free_y'。
To have only integer values on the axis, try using the package scales
, together with the argument labels
in scale_y_continuous()
. You can use one of the predefined formats that come with the package, or define your own, for example
要在轴上只有整数值,请尝试使用包标度以及scale_y_continuous()中的参数标签。例如,您可以使用程序包附带的预定义格式之一,或定义自己的格式
intfmt <- function(x) format(x, digits = 0)
will only print the integer part of a number. After specifying this, you just need to add the following line to your ggplot
call
只会打印数字的整数部分。指定后,您只需要在ggplot调用中添加以下行
+ scale_y_continuous(labels = intfmt)
#1
1
To manipulate the order of facets, you need the levels of the factor you are faceting on to be sorted as you want. So you need to order the levels of data
. This can be done for example like df$data <- factor(df$data, levels = c(...))
(with your desired order of levels in c()
).
要操纵构面的顺序,您需要根据需要对要面对的因子的级别进行排序。所以你需要订购数据级别。这可以例如像df $ data < - factor(df $ data,levels = c(...))(在c()中具有所需的级别顺序)。
Regarding your second question, look at ?facet_wrap
and in particular the scales
option. You will have to set scales = 'free'
or 'free_y'
.
关于你的第二个问题,请看?facet_wrap,特别是scale选项。您必须设置scales ='free'或'free_y'。
To have only integer values on the axis, try using the package scales
, together with the argument labels
in scale_y_continuous()
. You can use one of the predefined formats that come with the package, or define your own, for example
要在轴上只有整数值,请尝试使用包标度以及scale_y_continuous()中的参数标签。例如,您可以使用程序包附带的预定义格式之一,或定义自己的格式
intfmt <- function(x) format(x, digits = 0)
will only print the integer part of a number. After specifying this, you just need to add the following line to your ggplot
call
只会打印数字的整数部分。指定后,您只需要在ggplot调用中添加以下行
+ scale_y_continuous(labels = intfmt)