While at sea, press F2, go to "Relations", and change flag. Still in the F2 screen, switch to another tab - perhaps "Characters", then assign a perk, for example. Now switch to main view, and your flag is still the original one. And then go to F2 -> "Relations", which shows the flag to which you switched.
And that's why, having been near Guadeloupe but nowhere near the port, I raised a French flag, used "Sail-To" to get to the port (which was displayed on the "Sail-To" menu as the port, not the fort), then was attacked by the fort because the ship still showed a Spanish flag. (In the "Ardent" storyline, Spain is hostile to France.)
And that's why, having been near Guadeloupe but nowhere near the port, I raised a French flag, used "Sail-To" to get to the port (which was displayed on the "Sail-To" menu as the port, not the fort), then was attacked by the fort because the ship still showed a Spanish flag. (In the "Ardent" storyline, Spain is hostile to France.)