2024-06-28

R ggplot에서 geom_line() 사용할 때 x축이 자동 정렬 안되게 하려면

ggplot으로 그룹 별로 그래프 그리기 예제 에서 보이는 것 처럼, geom_line()을 쓰면 x값들을 자동으로 정렬해서 line을 그려줍니다. 


> df
  aa bb     cc
1  1 12  first
2  2 25  first
3  3 31  first
4  4 43  first
5  1 15 second
6  2 22 second
7  3 38 second
8  4 41 second

ggplot(df, aes(x=aa, y=bb)) + geom_line()



여기서, 정렬 되지 않은 원래의 data순서 그대로의 line을 보고 싶을 때가 있죠. 

그럴 때는 geom_line() 대신 geom_path()를 사용합니다. 

> ggplot(df, aes(x=aa, y=bb)) + geom_path()



두 개의 plot이 같은 data에서 나온 그래프임을 보여주기 위해서 point도 같이 그려보았습니다.

> ggplot(df, aes(x=aa, y=bb)) + geom_line() + geom_point()
> ggplot(df, aes(x=aa, y=bb)) + geom_path() + geom_point()



점들의 위치는 양쪽이 같네요. 하지만,  어떻게 연결해 주느냐에 따라 전혀 다른 plot으로 보입니다. 

다른 예를 하나 더 볼까요?

> theta <- seq(0, 2*pi, 1)
> df <- data.frame(x = cos(theta), y = sin(theta))
> ggplot(df, aes(x=x, y=y)) + geom_line()
> ggplot(df, aes(x=x, y=y)) + geom_path()





완전히 다르게 보이죠? geom_line()으로는 보이지않던 패턴이 geom_path()에서는 너무나 명확히 드러납니다.

어떻게 그래프를 그리는 지가 이렇게 중요하답니다. 



2024-06-25

R에서 ggplot으로 그룹 별로 그래프 그리기

ggplot2 라이브러리를 불러옵니다.

> library(ggplot2)

컬럼이 세 개인 데이터프레임을 하나 만듭니다.

df <- data.frame(aa=c(1,2,3,4,1,2,3,4), bb=c(12,25,31,43,15,22,38,41), cc=c("first", "first", "first", "first", "second", "second", "second", "second"))

df는 이렇게 생겼어요.


cc가 "first"인 데이터와 "second"인 데이터를 따로 두 개의 라인으로 그려보려고 합니다.


> ggplot(df, aes(x=aa, y=bb)) + geom_line()


흠...자동으로 sort가 되어서 하나의 라인만 그려지네요.

cc로 그룹을 만들려면 아래과 같이 group 키워드를 설정해줍니다.

> ggplot(df, aes(x=aa, y=bb, group=cc)) + geom_line()


두 개의 라인 그래프가 생겼습니다.

색깔을 넣어볼까요? 

color 키워드를 추가합니다. cc로 색을 정하겠다고 설정합니다.

> ggplot(df, aes(x=aa, y=bb, group=cc, color=cc)) + geom_line()



완성입니다! 

2024-06-23

What p-value can tell

In statistical hypothesis tests, the null hypothesis basically states that it is not extraordinary

For example, if a test is comparing the difference between two groups, the null is that there is no difference. When a test aims to see if a factor has a real impact on something, then the null is that there is no effect. 

The null means what happened in the outcome is not extraordinary, and it just happened due to a random fluctuation. The apparent difference in the two groups could be just a fluke. The seemingly impactful outcome could just happen by chance.

Whether the outcome happened by chance or not is decided by a p-value.

A p-value provides a quantity to tell how much the outcome is probable when the null is right. As far as the p-value is less than 5% (a conventional threshold), the null hypothesis can be rejected

Now that the null cannot explain the outcome, we can say that it is extraordinary: the difference is not a fluke, and the impact is not by chance, statistically speaking, it is statistically significant



2024-06-22

"in so far as" 뜻: --하는 한에서는

in so far as = to the extent/degree that

Insofar as I know, there are no other complications.
내가 아는 한에서는 다른 문제는 없다.
She has done her best to comfort him, insofar as she was able.
그녀가 할 수 있는 한에서는 그녀는 그를 달래주기 위해서 최선을 다했다.
The research suggests that the drug will be sucessful, in so far as one can draw conclusions from such a small sample size.
그 연구가 제안하는 것은, 그렇게 작은 샘플 사이즈로부터 결론을 내릴 수 있다고 한다면 그 약은 성공적이라는 것이다.
The news is good in so far as it suggests that a solution may be possible.
그 소식이 얘기하는 것처럼 어떤 해결 방법이 있는 한에서는, 그 소식은 좋은 소식이다. 
뭔가 확실하게 말하기는 어려울 때 약간 hedge하는 느낌인데요. 마지막 두 문장은 약간 부정적인 뉘앙스로 들리기도 합니다.



2024-06-21

R에서 ggplot 사용해서 그래프 그리기 (Plot with ggplot in R)

Install the ggplot2 package in Rstudio.

(Rstudio에서 ggplot2 패키지를 설치합니다.)




Click the "Install" button here.
(Install 버튼을 누르면 설치가 됩니다.)

Load the ggplot2 package in Rstudio Console.
(콘솔에서 ggplot2 패키지를 loading합니다.)



Make a simple data frame df with only 2 columns named x and y.
(데이터프레임 df를 만듭니다. 두 개의 컬럼으로 된 간단한 데이터프레임 입니다.)


Let's use ggplot!
(이제 ggplot을 사용해봅니다.)

Nothing happened. Because we've just regitered df to be used, but it's not used yet.
(아무것도 그려지지 않네요. 이제부터 df를 사용해서 뭘 좀 하겠다고 등록만 해 준 상태입니다.)


  Using aes(), add the information of which columns in df will be the x- and y-axes.
(df에서 어떤 컬럼을 사용할지 aes()를 이용해서 정보를 추가해줍니다.)


We've just registered the columns, so the axes and ranges are shown but still nothing appears.
(x축에 df의 x컬럼을 사용하고 y축에 df의 y컬럼을 사용하겠다고 등록만 된 상태입니다. 그래서 축과 범위는 데이타에 맞춰서 이미 만들어져있네요.)

Add the first geometric ogject. geom_point() addes points.
(ggplot의 geom(geometric object)를 추가해줍니다. geom_point()는 점을 추가하겠다는 뜻입니다.)


Yay, it's the first plot with ggplot!
(ggplot을 이용한 첫번째 plot이 그려졌네요!!)

For the line, use geom_line().
(선을 추가 하고 싶다면, geom_line()을 추가해주세요.)


In this way, you can add geometric objects.
(이런 식으로 원하는 geometric object를 추가해주면 됩니다.)

This is it. It's the basic way ggplot works!
여기까지가, ggplot의 기본 사용 방법입니다!


2024-06-19

How to schedule tasks running automatically in your Windows10

Make a batch file (ex. "test_batch.bat" and save it in the Desktop).


Start Task Scheduler and select Create Task.


In General tab, add Name.


In Trigger tab, select New.


In Edit Trigger, set the Settings, and click OK.


Now Trigger is set. 


In Actions tab, select New.


In Edit Action, add Program/script (ex. the "test_batch.bat" in your Desktop) and click OK.


Now the action is set. Click OK.


Now Task Scheduler shows the new task that has been added.


At the scheduled time, the batch file is runing automatically.


Enjoy your own scheduled tasks!