import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String [] args) {
FileWriter fw= null;
File file =null;
try {
file=new File("WriteFile.txt");
if(!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file);
fw.write("This is an string written to a file");
fw.flush();
fw.close();
System.out.println("File written Succesfully");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Один из способов - установить соотношение сторон графика, используя аргумент asp
, а затем рассчитать углы, используя указанные asp
:
asp <- 2
plot(1:10,1:10, asp=asp)
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=180/pi*atan(-1*asp))
abline(a=4, b=-2)
text(x=1, y=3, "reference line label", srt=180/pi*atan(-2*asp))
[/g0]
Установка другого asp
:
asp <- 0.8
plot(1:10,1:10, asp=asp)
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=180/pi*atan(-1*asp))
abline(a=4, b=-2)
text(x=1, y=3, "reference line label", srt=180/pi*atan(-2*asp))
[/g1]
Добавление к ответу @ Andrie: даже если вы не устанавливаете вручную par("asp")
, вы можете восстановить текущий рабочий формат со следующей функцией:
getCurrentAspect <- function() {
uy <- diff(grconvertY(1:2,"user","inches"))
ux <- diff(grconvertX(1:2,"user","inches"))
uy/ux
}
. Таким образом, вы можете создать свой plot: set asp <- getCurrentAspect()
; и приступаем к остальному решению @ Andrie.
Для всех, кого я знаю, эта функция существует где-то в экосистеме R, но я ее не видел ...
par("usr")
, но не мог найти ничего полезного.
– Andrie
1 August 2012 в 22:33
Аналогичное решение с ggplot2
data <- data.frame(x = 1:10, y = 1:10)
intercept <- 10
slope <- -1
ggplot(data, aes(x,y)) + geom_point(shape=1) +
geom_abline(intercept = intercept, slope = slope) +
geom_text(x=4, y=5, label="my label", angle=atan(slope)*180/pi)
[/g0]
intercept <- 10
slope <- -2
ggplot(data, aes(x,y)) + geom_point(shape=1) +
geom_abline(intercept = intercept, slope = slope) +
geom_text(x=4, y=5, label="my label", angle=atan(slope)*180/pi)
[/g1]
.rprofile
, которая удаляет тему по умолчанию ggplot2
, но здесь ничего другого здесь не так.
– Maiasaura
1 August 2012 в 22:35