visualization go wrong

ive use ggplot to create 4 line graph but it gone wrong
my code:

df_gtts %>%
filter(c==8|c==4|c==7|c==9) %>%
ggplot(aes(x=Vitri,y=GiatriTaiSan,group=1))+
geom_line()+
facet_wrap(~c)+
theme_bw()+
labs(title="Gia tri tai san cac nam 2021(8) - 2017(4) - 2020(7) - 2022(9)")


x is ok, but y is weird, as i sort y decreasing in data (GiatriTaiSan)

The problem is not so much in your charting code, as it is a problem of your data that you send to chart; you need to further modify your data before you ggplot;
you need to convert (mutate) GiatriTaiSan from being a character string that shows numbers with a comma format, to an actual number; you can use readr's readr::parse_number("54,321") for that sort of thing

it seems the data that should go on the y-axis are formated as character, not number.
Depending on where you got the data from, you may adjust the import step (maybe save it as excel and import the excel file) or adjust the encoding.
Alternatively you can convert it back to numbers, e.g. by removing the comma (as thousend separator) and then define it as numeric:

# some dummy data
df = data.frame(values = c("12,000", "5,000", "1,234"),
                vitri = c(1,2,3))

ggplot(df, aes (x = vitri, 
                y = values)) +
  geom_point() +
  geom_line(group = 1)
# this isn't working properly!

# convert to numbers
library(stringr)
df$num_values = as.numeric(str_remove(df$values, ","))

# check result
str(df) 
'data.frame':	3 obs. of  3 variables:
 $ values    : chr  "12,000" "5,000" "1,234"
 $ vitri     : num  1 2 3
 $ num_values: num  12000 5000 1234
# values are character (chr)
# num_values are numeric (num)

ggplot(df, aes (x = vitri, 
                y = num_values)) +
  geom_point() +
  geom_line(group = 1)

You can check your columns either with str(nameofyourdataframe)
or by hovering the mouse over the column name when looking at the dataframe.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.