简体   繁体   中英

R breaking linetype rule at axis line

I am trying to plot 2 variables using different linetypes based on a column called TB in my dataframe. Shown below is the code I've used:

q=ggplot()+geom_line(aes(deg_b,glim_f,colour=state,linetype=TB),min_splits_mp)+
  scale_linetype_manual(values=c(1,4))+
  geom_point(data = min_splits_mp, mapping = aes(x = deg_b, y = glim_f,colour=state, shape=TB,size=TB)) +
  scale_shape_manual(values=c(0,4))+
  scale_size_manual(values=c(3,4))+
  scale_color_manual(values=c("blue","red"))+
  ggtitle(paste(domain," Degredation vs best_split(Both tiebreaks)")) 
print(q)

The TB column is discrete.

When I run this code the following is plotted: 在此处输入图像描述

For completeness here is the data that I'm using:

      State         deg_b  glim_F   TB
1   7,5,9,2,1,3,6,0,8,4 0   3   Average case
2   6,2,4,8,3,0,5,9,1,7 0   9   Average case
3   6,2,4,8,3,0,5,9,1,7 0   10  Average case
4   6,2,4,8,3,0,5,9,1,7 0   9   Average case
5   6,2,4,8,3,0,5,9,1,7 0   10  Average case
6   7,5,9,2,1,3,6,0,8,4 1   5   Average case
7   6,2,4,8,3,0,5,9,1,7 1   5   Average case
8   7,5,9,2,1,3,6,0,8,4 2   5   Average case
9   6,2,4,8,3,0,5,9,1,7 2   5   Average case
10  7,5,9,2,1,3,6,0,8,4 3   5   Average case
11  6,2,4,8,3,0,5,9,1,7 3   5   Average case
12  7,5,9,2,1,3,6,0,8,4 0   9   Best TB
13  7,5,9,2,1,3,6,0,8,4 0   10  Best TB
14  7,5,9,2,1,3,6,0,8,4 0   8   Best TB
15  7,5,9,2,1,3,6,0,8,4 0   9   Best TB
16  7,5,9,2,1,3,6,0,8,4 0   10  Best TB
17  6,2,4,8,3,0,5,9,1,7 0   0   Best TB
18  6,2,4,8,3,0,5,9,1,7 0   1   Best TB
19  6,2,4,8,3,0,5,9,1,7 0   2   Best TB
20  6,2,4,8,3,0,5,9,1,7 0   3   Best TB
21  6,2,4,8,3,0,5,9,1,7 0   4   Best TB
22  6,2,4,8,3,0,5,9,1,7 0   5   Best TB
23  6,2,4,8,3,0,5,9,1,7 0   6   Best TB
24  6,2,4,8,3,0,5,9,1,7 0   7   Best TB
25  6,2,4,8,3,0,5,9,1,7 0   8   Best TB
26  6,2,4,8,3,0,5,9,1,7 0   9   Best TB
27  6,2,4,8,3,0,5,9,1,7 0   10  Best TB
28  6,2,4,8,3,0,5,9,1,7 0   0   Best TB
29  6,2,4,8,3,0,5,9,1,7 0   1   Best TB
30  6,2,4,8,3,0,5,9,1,7 0   2   Best TB
31  6,2,4,8,3,0,5,9,1,7 0   3   Best TB
32  6,2,4,8,3,0,5,9,1,7 0   4   Best TB
33  6,2,4,8,3,0,5,9,1,7 0   5   Best TB
34  6,2,4,8,3,0,5,9,1,7 0   6   Best TB
35  6,2,4,8,3,0,5,9,1,7 0   7   Best TB
36  6,2,4,8,3,0,5,9,1,7 0   8   Best TB
37  6,2,4,8,3,0,5,9,1,7 0   9   Best TB
38  6,2,4,8,3,0,5,9,1,7 0   10  Best TB
39  7,5,9,2,1,3,6,0,8,4 1   5   Best TB
40  7,5,9,2,1,3,6,0,8,4 1   4   Best TB
41  6,2,4,8,3,0,5,9,1,7 1   6   Best TB
42  6,2,4,8,3,0,5,9,1,7 1   5   Best TB
43  7,5,9,2,1,3,6,0,8,4 2   6   Best TB
44  7,5,9,2,1,3,6,0,8,4 2   5   Best TB
45  6,2,4,8,3,0,5,9,1,7 2   6   Best TB
46  6,2,4,8,3,0,5,9,1,7 2   5   Best TB
47  7,5,9,2,1,3,6,0,8,4 3   6   Best TB
48  7,5,9,2,1,3,6,0,8,4 3   5   Best TB
49  6,2,4,8,3,0,5,9,1,7 3   5   Best TB
50  6,2,4,8,3,0,5,9,1,7 3   4   Best TB

Notice that The best TB is supposed to have a broken dashed line between points(eg. the line in organge circle). However strangely this is not the case at deg_b=0 where the line between the blue X's is a dashed line that is not broken(See black circle). Why is this?

It's hard to be certain without seeing your data, but I'd guess the vertical blue line at deg_b=0 is the result of overplotting two or more lines that are slightly shifted (along the direction of the line) relative to each other, creating the appearance of dashes where each overplotted line alone is dot-dashed.

For example, note how part of the lower line in the graph below looks dashed, even though the two individual overplotted lines are dot-dashed:

d = data.frame(x = c(1, 5, 1, 4, 2, 6),
               y = c(1, 5, 2, 5, 2, 6),
               lt = rep(c("A","B","A"), each=2),
               group = rep(c("a","b","c"), each=2))

ggplot(d, aes(x, y, linetype=lt, group=group)) + 
  geom_line(size=0.5) +
  scale_linetype_manual(values=c(4,1)) + 
  theme_classic()

在此处输入图像描述

Also, you can shorten your code a bit as follows:

ggplot(min_splits_mp, aes(deg_b, glim_f, colour=state, linetype=TB)) +
  geom_line()+
  geom_point(aes(shape=TB, size=TB)) +
  scale_linetype_manual(values=c(1,4)) +
  scale_shape_manual(values=c(0,4)) +
  scale_size_manual(values=c(3,4)) +
  scale_color_manual(values=c("blue","red"))+
  ggtitle(paste(domain," Degredation vs best_split(Both tiebreaks)")) 

If you're going to use the same data frame in each geom , the you can just put the data frame in the main call to ggplot rather than repeating it in each geom. Likewise, aesthetic mappings (the things inside aes ) that apply to all geoms can also be put in the in main ggplot call, rather than repeating them in each geom. The only mapping that needs to be in geom_point alone is size=TB because you don't want the line width to change for different levels of TB .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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