how to plot this figure and why my codes are wrong?

data.frame(
         bill = c(1L,1L,2L,2L,3L,3L,4L,4L,5L,5L,
                  6L,6L,7L,7L,8L,8L,9L,9L,10L,10L,11L,11L,12L,12L),
        treat = c(0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,
                  0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,0L,1L),
  monthenergy = c(36.7032538284079,46.0402438841829,
                  32.9392795881018,41.2984949950169,28.1280279158878,
                  35.480098154243,24.9235110867169,31.8601494904683,25.8690610384381,
                  33.4558628130817,29.2486150406169,38.301071827994,
                  37.3012423685873,49.7570793648139,37.1106026686834,49.1615012450902,
                  38.0010307369504,50.4640419568872,25.6031441545062,
                  33.6860808231507,25.4171150782655,32.6026074847544,31.1551553278784,
                  39.2106574963103)
)

I need to draw a figure such that the x-axis is "bill" (12 months), y-axis is "monthenergy". And this figure should have 2 lines (treatment and control group). My codes are:

# draw the figure
x_min <- min(month_mean$bill)
x_max <- max(month_mean$bill)
month_mean %>% ggplot(aes(y=monthenergy, x=bill, color=factor(treat))) + 
  geom_point() +
  geom_line() +
  theme_minimal() +
  theme(plot.title = element_text(face = "bold", size = 14, hjust = 0.5),
        plot.subtitle = element_text(size = 12, hjust = 0.5)) + 
  labs(title = "Participants' and Nonparticipants' Average Energy Use in 2011",
       x="Month", y="Energy Use", color="Group") +
  scale_color_discrete(name="Group",
                       breaks=c("0","1"),
                       labels=c("Control","Treatment")) +
  scale_y_continuous(label=percent) +
  scale_x_date(labels = date_format("%h-%y"),
               breaks = seq(from = x_min, 
                            to = x_max,
                            by = "2 bill"))

I believe my codes contain more than 1 error.

Whats is the 2 bill in the last breaks?

2 + "2 bill"
#> Error in 2 + "2 bill": non-numeric argument to binary operator

Created on 2020-03-21 by the reprex package (v0.3.0)

Two suggestions:

scale_y_continuous(label=scales::label_percent())

and

breaks = seq(from = x_min, 
                            to = x_max,
                            by = 2))

but not sure how to fix

I want in the x-axis, it shows 1, 3, 5, 7.... not 1,2,3,4,5,6...

You're right.
scale_y_continuous(label=percent) is wrong. The label shouldn't be percent. Here should just be y value.

date_format("%h-%y") is also wrong, since bill is not month variable.

So I just delete these 2 codes. But it's still wrong.

You can't always get what you want.

"2 bill"

is a character string. How, exactly, do you plan on making it into something that can be used to increment a beginning number?

I figured it out. The codes should be:

month_mean %>% ggplot(aes(y=monthenergy, x=bill, color=factor(treat))) +
  geom_point() +
  geom_line() +
  theme_minimal() +
  theme(plot.title = element_text(face = "bold", size = 14, hjust = 0.5),
        plot.subtitle = element_text(size = 12, hjust = 0.5)) + 
  labs(title = "Participants' and nonparticipants' energy use in each month in 2011",
       x="month", y="energy use", color="Group") +
  scale_color_discrete(name="Group",
                       breaks=c("0","1"),
                       labels=c("Control","Treatment")) +
  scale_y_continuous(label=comma) +
  scale_x_continuous( breaks = seq(from = x_min, 
                            to = x_max, 
                            by = 2))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.